问题
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!
回答1:
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.
回答2:
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.
回答3:
"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.
回答4:
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.
回答5:
When we say one class extends another class it is having strong relation ship known as 'inheritance'.this means when one child extends parent then child should be able to inherit something from parent class like horse IS A animal .Horse is inheriting some properties of animal .But when a class implements another class then child class is trying to implement a contract dosen't need to inherit anything from parent just following a contract ,that why interface all methods are abstract by default but you can provide some concrete method in class(for child class to inherit) and can make some of then abstract is well .
So for me extends is inheritance and interface in implementing contract.hope this is satisfactory
回答6:
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: Test if object implements interface
回答7:
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.
回答8:
Book implements Taxable
Here also the relationship between Book
and Taxable
is
Book 'is a' Taxable
Refer this. You can see it says
When we talk about inheritance the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another.
来源:https://stackoverflow.com/questions/1050146/is-there-any-relation-between-the-class-that-implements-interface-and-that-inter