my dog learned polymorphism
The moose likes Beginning Java and the fly likes implementation inheritance vs type inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "implementation inheritance vs type inheritance " Watch "implementation inheritance vs type inheritance " New topic

implementation inheritance vs type inheritance

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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

Joined: Sep 18, 2014
Posts: 1
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."

The answer to "which is better" depends on what you're doing. Neither is universally best.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: implementation inheritance vs type inheritance
jQuery in Action, 3rd edition