views:

86

answers:

3
+1  Q: 

Proper inheritance

What is meant by proper inheritance?

+3  A: 

This thread gives a nice summary:

  • Proper inheritance occurs when the derived class "IS A" specialized type of the base class. Example Cat IS A Animal.

  • Improper inheritance occurs when a class is inherited from merely for code reuse without having any other relationship. Example Cat Inherits from Engine. A Cat is not an engine however both an engine and a cat purr.

Justin Ethier
I had checked it out but the example seemed weird.
ckv
A _nice_ summary? I second ckv's opinon... Truly, if this concept of inheritance propriety is effectively useful there must exist better sources which define, summarize and provide insight into it...
mjv
I meant the text I quoted is a nice summary :) - I agree with you there must exist better sources. It seems the problem here is that there is no official term "Proper Inheritance" just that if you are using inheritance correctly, you will be expressing an IS-A relationship.
Justin Ethier
A: 

When inheritance complies with a IS A relationship, as opposed to inheriting purely for code reuse without there being a logical subsumption of the child by the superclass.

Baxter
+1  A: 

I would like to add to what Justin and Baxter said.

The term proper inheritance is not really well-defined. Properly using inheritance is quite a subjective issue...

Consider the following example:

  • An interface: Bird
  • A concrete class: Ostrich

Should Ostrich inherits from Bird ? From a zoological point of view it makes sense, but from a Computer Science point of view... not so much. If Bird has a fly method, then how am I supposed to handle this in Ostrich::fly :x ?

There is somewhat of a war in the CS community. Indeed you'll regularly see books where Circle inherits from Ellipsis (or the other way around) when it doesn't really makes sense from a CS point of view.

So my own little definition:

Considering that the interface defines precise semantics for each of its methods, a concrete class should only inherit from the interface if the implementation of each of the method matches the semantics specified.

Matthieu M.