Java Interfaces – Is Implementing an Interface Called Inheritance?

definitioninheritanceinterfacesjavaterminology

If my class implements an interface then can I say that I'm following inheritance? I know that when a class extends another class then it's inheritance.

Best Answer

UPDATE: I've revised this answer. A number of good points were raised in the comments that deserved calling out.

If my class implements an interface then can I say that I'm following inheritance?

It is not entirely clear what you mean by "following inheritance". Let's ask a slightly different question?

What is inheritance?

  • When members of one type X are considered to be members of another type Y, those members of Y are inherited from X.
  • There is an inheritance relationship between some types; that is, for some types X and Y we say "Y inherits from X".

These are subtly different. That is unfortunate because it is confusing.

What confusions typically arise from this subtle distinction?

Confusion may arise because people think of inheritance as a mechanism for sharing implementation details. Though it is such a mechanism, that mechanism works by sharing members. Those members need not have implementations! As we will see, they can be abstract.

I personally would be happier if the Java and C# specifications used a word other than "inherits" to describe the relationship between interface methods and classes, to avoid this confusion. But they do not, and we have to reason from the specifications, not against them.

In Java, are interface members inherited by classes which implement them?

Yes, some are. See the Java specification section 8.4.8, which I quote here for your convenience.

A class C inherits from its direct superclass and direct superinterfaces all abstract and default methods m for which all of the following are true: [...]

If you say that a class implements an interface then the class inherits the abstract and default methods of that interface. (Of course I have omitted the conditions which follow; see the specification for details. In particular, a class which implements a member of an interface is not considered to have inherited that member. Again, is this confusing? Yes.)

Do we typically say in Java that a class inherits from an interface?

Typically we would say that a class implements an interface. As noted above, a class may inherit members from an interface, and yet still not be said to inherit from the interface. Which is confusing, yes.

Does this subtle distinction matter in day-to-day work?

Typically not. This sort of narrow parsing of the specification is more useful to compiler writers than line of business developers. Its more important to understand when to use an interface than it is to get a precise definition of "inherits from".

Related Topic