Inheritance in JAVA

Inheritance in JAVA

Inheritance is the mechanism in java by which one class is allowed to inherit the features(methods and fields) of another class.

The class which inherits the properties of other is known as SUB CLASS (derived class/child class) and the class whose properties are inherited is known as SUPER CLASS (base class/parent class).

Inheritance defines is-a relationship between a Super class and its Sub class.

Types Of Inheritance

1. Single Inheritance

In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B.

Single_inheritance_java

Example, demonstrating single inheritance is given below.

Save file as Main.java

class Main{
    public static void main(String args[])
    {
       Area a = new Area();

       //Take input

       a.input(10,50);

    //Calculate the area
      int ans = a.calculateArea(); 
      System.out.println("Area of rectangle = "+ans);

   }
}

class Data
{
    int length,breadth,area;
public void input(int l, int b)

   {
length = l;
breadth = b;

   }
}

class Area extends Data
{
    public int calculateArea()
    {  
        area = length*breadth;
        return area;
    }
}

Output

Area of rectangle = 500

2. Multilevel Inheritance

In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, 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. In Java, a class cannot directly access the grandparent’s members(i.e class A is the grandparent for Class C).

multilevel_inheritance_java

Example, demonstrating multilevel inheritance is given below.

Save file as Main.java

class Main{
    public static void main(String args[])
    {
       Volume v = new Volume();

       //Take input

       v.input(5,15,2);

    int ans = v.calculateArea(); 
      System.out.println("Area of rectangle = "+ans);

      System.out.println("Volume of rectangle = "+v.calculateVolume());

   }
}

class Data
{
    int length,breadth,height,area,volume;
public void input(int l, int b, int ht)

   {

       length = l;
       breadth = b;
       height = ht;

  }
}

class Area extends Data
{
    public int calculateArea()
    {  
        area = length*breadth;
        return area;
    }
}

class Volume extends Area
{
    public int calculateVolume()
    {  
        volume = area*height;
        return volume;
    }
}

Output

Area of rectangle = 75

Volume of rectangle = 150

3. Hierarchical Inheritance

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class.In below image, the class A serves as a base class for the derived class B and C.hierarchical_inheritance_java

Example, demonstrating hierarchical inheritance is given below.

Save file as Main.java

class Main{
    public static void main(String args[])
    {
        B b = new B();
        C c = new C();

        b.print();
        c.print();
    }
}

class A{
    void print(){
        System.out.println("Your Parent Class is A");
    }
}

class B extends A{
    void print(){
        System.out.println("This is Child Class B and ");
        super.print();   //Used to call parent class method
    }
}

class C extends A{
    void print(){
        System.out.println("This is Child Class C and ");
        super.print();   //Used to call parent class method
    }
}

Output

This is Child Class B and
Your Parent Class is A
This is Child Class C and
Your Parent Class is A

4. Multiple Inheritance

In Multiple inheritance, we can have more than one superclass, i.e. a child/derived class can have two parent/base classes

Note: Java does not support multilevel inheritance, but we can achieve it by implementing interfaces.

multiple inheritance

5. Hybrid Inheritance

It is a combination of two or more of the above types of inheritance.

Note: As Java does not support multilevel inheritance, hence hybrid inheritance is also not possible, but we can achieve the same using interfaces in java.

hybrid inheritance

Share Me!
  •  
  •  
  •  
  •  
  •  
  •