This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implementation inheritance vs type inheritance

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i came across these two terms, implementation inheritance and type inheritance, while going throught the Timestamp API.
can anyone please explain what do they actually mean.
my inference is that implementation inheritance is using interfaces implementation and type inheritance is sub classing. am i right.
what is the meaning and signigifance of these two concepts and whcih is better to use..?
 
Alan Colette
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're close, but I think you have it backwards.

"An object's class defines how the object is implemented. In contrast, an object's type only refers to its interface. Class inheritance defines an object's implementation in terms of another object's implementation. Type inheritance describes when an object can be used in place of another."
Source

The answer to "which is better" depends on what you're doing. Neither is universally best.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, there is really only one way to do inheritance in Java: by creating a subtype of some type (creating a subclass, or implementing an interface).

The terms "type inheritance" and "implementation inheritance" are more about why you are using inheritance. Type inheritance means that your intention is to create a new type, which is a specialization of its supertype. Implementation inheritance means that you are using inheritance because you want to reuse code in the superclass for multiple subclasses, so that you don't have to copy and paste the same code in the subclasses.

In my opinion, using inheritance purely because you want to reuse code is not a good idea. There are other ways to reuse code (for example by using composition instead of inheritance).

In the standard JDK classes you can find examples of how inheritance is misused in this way, leading to badly designed classes. One example is the class java.sql.Timestamp. It inherits from java.util.Date, but users should not regard a java.sql.Timestamp as if it's a subtype of java.util.Date, as its API documentation explains:
Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.

This is just very bad design - it makes it very easy for users of class java.sql.Timestamp to make mistakes and use it in a way that it was not intended.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic