5

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!

Eddie
  • 53,828
  • 22
  • 125
  • 145
Johanna
  • 27,036
  • 42
  • 89
  • 117

8 Answers8

8

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
  • 196,001
  • 113
  • 385
  • 569
  • 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 Jun 26 '09 at 17:39
5

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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.

Malcolm
  • 41,014
  • 11
  • 68
  • 91
3

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
  • 38,619
  • 8
  • 86
  • 96
2

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

JavaCreature
  • 191
  • 1
  • 1
  • 6
1

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

Community
  • 1
  • 1
cgp
  • 41,026
  • 12
  • 101
  • 131
0

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
  • 39,912
  • 17
  • 102
  • 155
0
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.

Kadiam
  • 303
  • 4
  • 12