views:

342

answers:

6

Consider this class hierarchy:

  • Book extends Goods
  • Book implements Taxable

As we know, there is a relationship between a subclass and its superclass (is-a).

Q: Is there any relationship like "is-a" between Book and Taxable?

GOOD Answers, but you said that "is-a" is also a relationship between Book and Taxable, but "is-a" is a relation between classes, and an interface is not a class!

+3  A: 

"Behaves like..."

That's what what I would say. Not is something, but behaves like something. Or as an alternative "can something", but that's more specific than behaviour.

Malcolm
+6  A: 

Yes. The relationship is exactly the same

Book is a Taxable too.

EDIT

An interface is an artifact that happens to match Java's ( and probably C# I don't know ) interface keyword.

In OO interface is the set of operations that a class is 'committed' perform and nothing more. Is like a contract between the object class and its clients.

OO programming languages whose don't have interface keyword, still have class interface OO concept.

OscarRyz
An interface is essentially a different way to implement a class relationship, so is-a is absolutely appropriate. It may delegate to an internal (has-a) class though--not sure what you'd call that, but I think it's still is-a because your class is still behaving as the interface specifies.
Bill K
+5  A: 

Well there's "supports-the-operations-of". Personally I don't find the "is-a", "can-do" etc mnemonics to be terribly useful. I prefer to think in terms of what the types allow, whether they're specialising existing behaviour or implementing the behaviour themselves etc. Analogies, like abstractions, tend to be leaky. If you know what the different between interface inheritance and implementation inheritance is, you probably don't need any extra phraseology to express it.

Jon Skeet
And don't forget that "instanceof" will work for either a base class or interface.
GalacticCowboy
And if the interface just notes some property and doesn't do any specific operations?
Malcolm
@Malcolm: Shouldn't matter - if I implement an interface, I am an instance of it.
GalacticCowboy
Right, but Jon Skeet stated "supports-the-operations-of", and it doesn't support any new operations, just has a new property.
Malcolm
I'd say that fetching the value of a property is an operation. It's something you can do with the type, isn't it?
Jon Skeet
If it includes participating in some kind of operations, then this is OK. Maybe you should clarify it a bit, but that's, of course, up to you.
Malcolm
+1  A: 

This should do:

public static boolean implementsInterface(Object object, Class interf){
    return interf.isInstance(object);
}

For example,

 java.io.Serializable.class.isInstance("a test string")

evaluates to true.

from: http://stackoverflow.com/questions/766106/test-if-object-implements-interface/768633

altCognito
+2  A: 

the relationship would be as stated: 'implements'

these relationship names spring from usage in sentences. "Book 'is-a' Goods" can be written without the quotes and hyphen and it makes sense. similarly, Book 'implements' Taxable can be written without the quotes.

akf
A: 

What's all the excitement about? The multiple question marks and multiple exclamation points?

Does it bother you that we can say a Book is Taxable, even though Taxable is an interface? Please calm down.

There are different keywords in the language for a class' relationship to an interface and to a superclass, but the conceptual nature of that relationship is the same, therefore it's entirely reasonable to use the same English terms to describe it. A Book is Taxable, just as a Book is a Good. To bring the terms even closer, a Book is a TaxableItem. It's OK.

Carl Manaster