Inheritance:
Let's try to formalize the term ‘inheritance’:
Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say A inherits from B. Objects of class A thus have accesses to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance.
If class A inherits from class B, then B is called super class of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding super class are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the super class.
In the literature you may also find other terms for super class and subclass. Super classes are also called parent classes. Subclasses may also be called child classes or just derived classes.
Of course, you can again inherit from a subclass, making this class the super class of the new subclass. This leads to a hierarchy of super class/subclass relationships. If you draw this hierarchy you get an inheritance graph.
We've defined a class to be a definition, or blueprint, from which object oriented instances are created. A stereo is an instance of the stereo class. But classes themselves can be defined as specializations of other classes. For example, if you didn't know what a stereo was, you would probably understand if I told you that it was a hand held MP3 player. In fact, all handheld MP3 players share a certain number of characteristics. Like a stereo, a RIO can hold and play MP3 files downloaded from a computer. It can't hold as many songs as the stereo, but at least some of the functionality is the same. Hence the RIO is a subclass of the super class stereo or RIO is inherited from the class stereo.
A common drawing scheme is to use arrowed lines to indicate the inheritance relationship between two classes or objects. In our examples we have used “inherits-from”. Consequently, the arrowed line starts from the subclass towards the super class as illustrated below-

Figure: I (1)
Inheritance means that the language gives you the ability to extend or enhance existing objects. The inherited class i.e. new class will have the combined features of both of the classes.
Let us review the whole thing again since it is perhaps the most important topic in OOP. Generally speaking, objects are defined in terms of classes. You know a lot about an object by knowing its class. Even if you don't know what a penny-farthing is, if I told you it was a bicycle, you would know that it had two wheels, handle bars, and pedals.
Object-oriented systems take this a step further and allow classes to be defined in terms of other classes. For example, mountain bikes, racing bikes, and tandems are all kinds of bicycles. In object-oriented terminology, mountain bikes, racing bikes, and tandems are all sub class of the bicycle class. Similarly, the bicycle class is the super class of mountain bikes, racing bikes, and tandems. This relationship is shown in the following figure.

Each subclass inherits state (in the form of variable declarations) from the super class. Mountain bikes, racing bikes, and tandems share some states: cadence, speed, and the like. Also, each subclass inherits methods from the super class. Mountain bikes, racing bikes, and tandems share some behaviour: braking and changing pedalling speed, for example.
However, subclasses are not limited to the state and behaviors provided to them by their super class. Subclasses can add variables and methods to the ones they inherit from the super class. Tandem bicycles have two seats and two sets of handle bars; some mountain bikes have an extra set of gears with a lower gear ratio.
Subclasses can also override inherited methods and provide specialized implementations for those methods. For example, if you had a mountain bike with an extra set of gears, you would override the "change gears" method so that the rider could use those new gears.
You are not limited to just one layer of inheritance. The inheritance tree, or class hierarchy, can be as deep as needed. Methods and variables are inherited down through the levels. In general, the farther down in the hierarchy a class appears, the more specialized is its behavior.
The
Object
class is at the top of class hierarchy, and each class is its descendant
(directly or indirectly). A variable of type
Object
can hold a reference to any object, such as an instance of a class or an array.
Object
provides behaviors that are required of all objects running in the Java Virtual
Machine. For example, all classes inherit
Object's
to String
method, which returns a string representation of the object.
Inheritance offers the following benefits:
· Subclasses provide specialized behaviours from the basis of common elements provided by the super class. Through the use of inheritance, programmers can reuse the code in the super class many times.
· Programmers can implement super classes called abstract classes that define "generic" behaviours. The abstract super class defines and may partially implement the behaviour, but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.
One important object-oriented mechanism is multiple inheritance. Multiple inheritance does not mean that multiple subclasses share the same super class. It also does not mean that a subclass can inherit from a class which itself is a subclass of another class.
Multiple inheritances mean that one subclass can have more than one super class. This enables the subclass to inherit properties of more than one super class and to ``merge'' their properties. The following is an example of deriving a draw able string which inherits properties of Point and String. This ‘DrawableString’ is an example of multiple inheritance that inherits from ‘Point’ and ‘String’.

Page-5