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.
It is not entirely clear what you mean by "following inheritance". Let's ask a slightly different question?
These are subtly different. That is unfortunate because it is confusing.
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.
Yes, some are. See the Java specification section 8.4.8, which I quote here for your convenience.
If you say that a class implements an interface then the class inherits the abstract and 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.)
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.
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
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:
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. |
|||
|
protected by Thomas Owens♦ yesterday
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?