Inheritance in Java

In this tutorial, we will learn about Inheritance in Java. Inheritance is a mechanism in which we can inherit properties(methods and variable) of one class into another. This helps us to create a new class which will have its own properties and properties of its parent class(from which new class is inheriting features). While performing inheritance you will see two kinds of classes as follow:

  • Parent Class: The which being inherit.
  • Child Class: The class which is inheriting features of another class:

It is important to note, we use extends keyword to perform inheritance in Java.

Types of Inheritance in Java:

There is a total of three types of inheritance available in Java. These types are on the basis of order in which classes being inherited.

  • Single Level Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance

I know students who have background in c++ might be missing Multiple Inheritance here. But Multiple Inheritance in classes is not available in Java.

Single Level Inheritance:

When a class inherit another class this is known as a single level inheritance in Java. As in given illustration you can see class B is inheriting to class A.

Inheritance in Java

Example Program:

class A{
  public void showA(){
    System.out.println("I am show method in A Class");
  }
}
public class B extends A{
  public void showB(){
    System.out.println("I am show method in B Class");
  }
  
  public static void main(String args[]){
    B obj=new B();
    obj.showA();
    obj.showB();
  }
}

Multi-Level Inheritance:

When inheritance performs in multiple classes and at several levels. This is known as Multi-Level inheritance in Java. As in given illustration you can see class B is inserting to class A and class C is inheriting to class B.

In this case class C will have features of class A and class B because class C is inheriting to class B which already have features of class A.

Inheritance in Java

Example Program:

class A{
  public void showA(){
    System.out.println("I am show method in A Class");
  }
}
class B extends A{
  public void showB(){
    System.out.println("I am show method in B Class");
  }
}
  
public class C extends B{
  public void showC(){
    System.out.println("I am show method in C Class");
  }
  
  public static void main(String args[]){
    C obj=new C();
    obj.showA();
    obj.showB();
    obj.showC();
  }
}

Hierarchical Inheritance

When multiple classes inherit to single class it is known ad Hierarchical Inheritance. As in given illustration you can see class B and class C both are inheriting to class A.

Inheritance in Java

Example Program:

class Greeting{
   public void welcome(String str){
     System.out.println("Hello, We welcome you "+str);
   }
}

class Teacher extends Greeting{
   public static void main(String args[]){
     new Teacher().welcome("Sir");
   }
}

class Student extends Greeting{
   public static void main(String args[]){
     new Student().welcome("dear Student");
   }
}

In the above example, I have used an anonymous object. If don’t have any idea about the anonymous object. Click here to learn.

What is Inheritance?

Inheritance is a mechanism in which we can inherit properties(methods and variable) of one class into another. This helps us to create a new class which will have its own properties and properties of its parent class

What are Types of Inheritance?

There is a total of three types of inheritance available in Java.

Single Level Inheritance
Multi-Level Inheritance
Hierarchical Inheritance

Is Multiple Inheritance available in Java?

No, Multiple Inheritance is not available in Java.

Spread the love