In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes. A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

In simple words, inheritance means – one can use anything of anyone. Just like you can use anything of your parents.

Syntax –

class base{
variables; methods;
}
class child extends base{
variables;
methods;
}

Note

To do the Inheritance, we need to use of ‘extends’ keyword.
The ‘extends’ keyword will be use for only child class.
say , class B inherited by class A, means class B able to use variables and methods(functions) of class A.
example  –
class A{
variable1, method1 ;
}
class B extends A{
variable1 , method1, method2;
}

There are multiple inheritance are there in java

  • Single Inheritance
  • Multiple Inheritance
  • Multi-level Inheritance
  • Hybrid Inheritance

Single Inheritance

Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code.

Let’s understand through below code…

class A{
    public void run(){   
    System.out.println(“this is class A”); 
  }
}
class B extends A /* here child class B inherit class A. */

  public void run(){     
  System.out.println(“this is class B”); 
  }
}
public class single {   
public static void main(String[] args) {     
  B b1 = new B();     
  b1.run(); 
  // we accessing class ‘A’ member functions through constructor B()
  A a1 = new B();
  a1.run();
  }
  }
output

Multiple Inheritance

Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclass’s and subclass.

Let’s understand through below code…

class A{
    public void run(){
        System.out.println(“this is class A”); 
  }
}
class B extends A{ 
  public void run(){     
  System.out.println(“this is class B”);
    }
}
class C extends A{ 
  public void run(){     
  System.out.println(“this is class C”); 
  }
}
class D extends B{ 
  public void run(){     
  System.out.println(“this is class D related with B”); 
  }
}
public class multipleInherit { 
  public static void main(String[] args) {   
   D d1 = new D();   
   d1.run();     
 C c1 = new C();   
   c1.run(); 
  } }
output

Multi-level Inheritance

It involves inheriting a class, which already inherited some other class. Correlating it with a real-life scenario, we’ve often seen some of our habits and thoughts match precisely with our parents. And similarly, their habits match with their parents, i.e., our grandparents. We can create hierarchies in Java with as many layers of Inheritance as we want. This means we can utilize a subclass as a superclass. In this case, each subclass inherits all of the traits shared by all of its superclass’s.

Let’s understand through code below…

class A{
    public void run(){ 
      System.out.println(“this is class A”);
    }
}
class B extends A // here child class B inherit class A.

  public void run(){     
  System.out.println(“this is class B”);
    }
}
class C extends B // here child ‘C’ inherit parent class B.

  public void run(){     
  System.out.println(“this is class C”); 
  }
}
public class multiLevel { 
  public static void main(String[] args) {     
  C c1 = new C();     
  c1.run(); 
  A a1 = new B();
  a1.run();
  B b1 = new C();
  b1.run();
 
  } }
output

Hybrid Inheritance

A hybrid inheritance is a combination of more than one types of inheritance.

class A{
    static void msg(){
      System.out.println(“this is class A”);
    }
  }
  class AB extends A{
    static void msg2(){
      System.out.println(“this is class AB extends parent class A”);
    }
  }
  class ACP extends AB{
    static void msg3(){
      System.out.println(“child class ACP extends parent class AB,grant parent A”);
    }
  }
  class AC extends A{
   static void msg4(){
      System.out.println(“child class AC extends parent class A”);
    }
  }
  class kali extends AC{
    static void msg5(){
      System.out.println(“child class kali parent AC, grant parent A”);
    }
  }
  class arch extends AC{
    static void msg6(){
      System.out.println(“child class arch parent AC, grand parent A”);
    }
  }
  class nmap extends kali{
    static void msg7(){
      System.out.println(“child class nmap, parent class kali,grant parent AC,A”);
    }
  }
  class johnripper extends kali{
    static void msg8(){
      System.out.println(“child class johnripper, parent class kali,grand-parent AC,A”);
    }
  }
 class HybridInherit{
  public static void main(String[] args) {
    johnripper john= new johnripper();
    john.msg8();
    john.msg5();
    john.msg();
 
    ACP acp= new ACP();
    acp.msg3();
    acp.msg2();
  }
 }
output