07-07-2012, 04:40 PM
Inheritance application
Applications
Inheritance is used to co-relate two or more classes to each other. With the use of inheritance we can use the methods and the instance variables of other classes in any other classes.
Overriding
Many object-oriented programming languages permit a class or object to replace the implementation of an aspect—typically a behavior—that it has inherited. This process is usually called overriding. Overriding introduces a complication: which version of the behavior does an instance of the inherited class use—the one that is part of its own class, or the one from the parent (base) class? The answer varies between programming languages, and some languages provide the ability to indicate that a particular behavior is not to be overridden and behave according to the base class. For instance, in C#, the overriding of a method should be specified by the program. An alternative to overriding is hiding the inherited code.
Code reuse
One of the earliest motivations for using inheritance was the reuse of code that already existed in another class. This practice is usually called implementation inheritance. Before the object-oriented paradigm was in use, one had to write similar functionality over and over again. With inheritance, behaviour of a superclass can be inherited by subclasses. It not only possible to call the overridden behaviour (method) of the ancestor (superclass) before adding other functionalities, one can override the behaviour of the ancestor completely.
For instance, when programming animal behaviour, there may be the class of bird, of which all birds are derived. All birds may use the functionality of flying, but some may fly with a different techniques (swinging, using thermic winds like Albatroses). So, flying bird may use all the behaviour of birds, or call it and add some other behaviour for the bird species. And some that cannot fly anymore, like kiwis, would override it with a method having no behaviour at all.
In most quarters, class inheritance for the sole purpose of code reuse has fallen out of favor.The primary concern is that implementation inheritance does not provide any assurance of polymorphic substitutability—an instance of the reusing class cannot necessarily be substituted for an instance of the inherited class. An alternative technique, delegation, requires more programming effort, but avoids the substitutability issue. In C++ private inheritance can be used as form of implementation inheritance without substitutability. Whereas public inheritance represents an "is-a" relationship and delegation represents a "has-a" relationship, private (and protected) inheritance can be thought of as an "is implemented in terms of" relationship.
Object Oriented-Software Construction, 2nd edition by Bertrand Meyer, the creator of the object-oriented programming language Eiffel, lists twelve different uses of inheritance, most of which involve some amount of implementation inheritance.