• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

implementation inheritance vs type inheritance

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic