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.
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".
Inheritance means writing a new subclass for a superclass. Writing a new class against an interface is implementing that interface. (And writing a new interface based on an old one is extending that interface.)
The only correct term that applies to all three possibilities is subtyping. Not every subtype is a subclass.
With subclasses, you
- inherit state of the superclass (all instance variables, whether you see them or not)
- inherit actual implementations (all non-abstract methods)
With interfaces, you fullfill a contract by implementing the declared methods.
That's the classical way to look at it. Now with Java 8 the interfaces become a mixture:
- you still don't inherit state (as interfaces still don't have instance variables)
- you now can inherit default implementations from the interface
Would implementing an interface whose methods all have default implementations still count as 'implement', or rather as extension? I couldn't tell. As this case is rather far-fetched (this actually enabled stateless multi-inheritance), I'd still use 'inheritance' only with subclasses.