What is meant by proper inheritance?
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.
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.
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.