Course Content‎ > ‎Session 5‎ > ‎

Inheritance

Reusability is a very important aspect of OOP. It is always better to reuse something that already exists rather than creating the same all over again. Java supports this concept and Java classes can be reused in several ways. This is done by creating new classes that use the properties of the existing classes. The concept of deriving a new class from an existing class is called as inheritance. The existing class is called as the base class or parent class or super class and the new class is called as the subclass or derived class or child class.  Inheritance allows subclasses to inherit all the variables and methods of their parent classes.  Inheritance can be: 
 
    1. Single inheritance
    2. Multilevel inheritance
    3. Hierarchical inheritance  
    4. Multiple inheritance  
 



 

NOTE: Java does not directly implement multiple inheritance. But this concept is implemented using a secondary inheritance path in the form of interfaces (discussed later).
 
Defining a Subclass
 
To define a subclass, the syntax is:
 
    class subclassname extends superclassname
    {
        variable declaration;
        method declaration;
    }  

The keyword extends signifies that the properties of the superclassname are extended to subclassname. The subclass will now contain its own variables and methods as well as those of the superclass.  Use the concept of inheritance, when you want to add some more properties to an existing class without actually modifying it. Here is the code of a program, which explains the concept of single inheritance:
 
    /* definition of class Room */
    class Room           
    {
        int length;
        int breadth;
        Room (int x, int y)
        {
            length = x;
            breadth = y;
        }
        
        int area( )
        {
            return (length * breadth);
        }
    }
    //class BedRoom inherits the class Room  
    class BedRoom extends Room 
    {
        int height;
        BedRoom (int x, int y, int z)
        {  
            // pass values to the constructor of super/base class
            super(x, y);   i.e. Room
            height = z;
        }
        
        int volume( )
        {
            return (length * breadth * height);
        }
    }
    
    class Inheritest
    {
        public static void main (String args[ ])
        {
            // room1 is object of class BedRoom  
            BedRoom room1 = new BedRoom (14, 12, 10);      
            
            // area( ) is a method of super class Room inherited by subclass BedRoom
            int area = room1.area( );     
            
            // volume( ) is a method of subclass BedRoom  
            int volume = room1.volume( ); 
            System.out.println("Area is " + area);
            System.out.println("Volume is " + volume);
        }
    }
 
Output of this program:
    Area is 168
    Volume is 1680
 
The above program defines a class Room and extends it to another class BedRoom. Class BedRoom defines its own data members and methods also. Therefore the subclass BedRoom now includes three data members i.e. length, breadth and height and two methods are: area( ) and volume( ). The statement
 
    BedRoom room1 = new BedRoom(14, 12, 10);

first calls the BedRoom constructor, which in turn calls  the Room constructor by using the keyword super.
 
    BedRoom(int x, int y, int z)
    {  
        /* to pass the values to the constructor of the base class */
        super (x,y); 
        height = z;
    }
    
Finally the object room1 of the subclass BedRoom calls the method area( ) defined in the super class as well as the method volume() defined in the subclass itself.
 
Subclass Constructor
 
A subclass constructor is used to construct the instance variables of both the subclass and the superclass. The subclass uses the keyword super to invoke the constructor of the superclass. The following points should be noted, while using the keyword super:
 
  1. super may be used only within a subclass constructor.
  2. The call to superclass constructor may appear as the first statement within the subclass constructor.
  3. The parameters in the super call must match the order and type of instance variable declared in the superclass.
 
Multilevel Inheritance

Just like other OOP language, Java also supports the concept of using a derived class as a super class. This concept has also been used in Java in building its class library.                 


 
In the above diagram, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class B inherits the members of class A, where as class C inherits the members of both class A and class B.
 
    class A
    {
           ...........
           ...........
    }
    class B extends A
    {
           ...........
           ...........
    }
    class C extends B
    {
           ...........
           ...........
    }

Multilevel inheritance can be extended to any number of levels.
 
Hierarchical Inheritance
 
Another interesting application of inheritance is to use it as a support to the hierarchical design of the program. In this a problem is cast into a hierarchy, where certain features of one level, are shared by others below that level. Given below is an example, which shows a hierarchical classification of accounts in a commercial bank:


Hierarchical classification of bank accounts
Comments