Back Button
Back to Chapter
Introduction
No items found.
Collapse_Icon
Table of Contents

Let's consider a scenario, Father as parent class and Son as child class. Son acquires the properties like color, a character from Father. Hence, it is called inheritance.

Inheritance in Java is a concept that acquires the properties from one class to another class. A class can inherit attributes and methods from another class. The main advantage of inheritance is code reusability.

The class that inherits the properties is known as the sub-class or the child class. The class from which the properties are inherited is known as the superclass or the parent class.

Syntax for child-class:

class child_classname extends parent_classname{

           variables declaration;
           methods declaration;
}

Syntax of Inheritance:

class parent_class
{
// parent_class data variables
// parent_class member functions
}
class child_class extends parent_class
{
// child_class data variables
// child_class member functions
}

/*Inheritance uses the “extends” keyword to create a derived class
by reusing base class code. */

Types of Inheritance

The different types of Inheritance are:

  • Single Inheritance
  • Multiple Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

i) Single Inheritance:

When a class inherits another class, it is known as single inheritance. In the example given below, the Son class inherits the Father class, so there is single inheritance.

Example:

import java.lang.*;
class Father { // Base class
   void work() {
       System.out.println("working...");
   }
}
class Son extends Father { // Derived class
   void play() {
       System.out.println("playing...");
   }
}
class Inheritance {
   public static void main(String args[]) {
       Son s = new Son();
       s.play(); // inherited from derived class
       s.work(); // inherited from base class
   }
}

/*OUTPUT:
playing...
working...  */

ii) Multilevel Inheritance:

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, Son class inherits the Father class which again inherits the Grandfather class, so there is a multilevel inheritance.

Example:

import java.lang.*;
class Grandfather { // Base class
   void sleep() {
       System.out.println("sleeping...");
   }
}
class Father extends Grandfather { // Derived class 1
   void work() {
       System.out.println("working...");
   }
}
class Son extends Father { // Derived class 2
   void play() {
       System.out.println("playing...");
   }
}
class Inheritance{
   public static void main(String args[]) {
       Son s = new Son();
       s.sleep(); // inherited from base class
       s.work();  // inherited from derived class 1
       s.play();  // inherited from derived class 2
   }
}
/*OUTPUT:
sleeping...
working...
playing... */

iii) Hierarchical Inheritance:

When two or more classes inherit a single class, it is known as hierarchical inheritance. In the example given below, Son and Daughter classes inherit the Father class, so there is hierarchical inheritance.

Example:

import java.lang.*;
class Father {  // Base class
   void work() {
       System.out.println("working...");
   }
}
class Son extends Father { // Derived class 1
   void play() {
       System.out.println("playing...");
   }
}
class Daughter extends Father { // Derived class 2
   void learn() {
       System.out.println("learning...");
   }
}
class Inheritance{
   public static void main(String args[]) {
       Son s = new Son();
       s.work(); // inherited from base class
       s.play(); // inherited from derived class 1
       //s.learn(); shows error because it is the method of another derived class  
   }
}
/*OUTPUT:
playing...
working... */

iv) Hybrid Inheritance:

A hybrid inheritance is a combination of more than one types of inheritance. In the example given below, Son and Daughter classes inherit the Father class, so there is hierarchical inheritance and Father class is inherited from the Grandfather class ,so it is single inheritance. Hence it is a combination of single inheritance and hierarchical inheritance.

Example:

import java.lang.*;
class Grandfather {
   public void sleep() {
       System.out.println("sleeping...");
   }
}

class Son extends Father {
   public void play() {
       System.out.println("playing...");
   }
}

class Daughter extends Father {
   public void read() {
       System.out.println("reading...");
   }

}

class Father extends Grandfather {
   public void work() {
       System.out.println("working...");
   }
   public static void main(String args[]) {

       Father obj = new Father();
       obj.work(); // inherited from class Father
       obj.sleep();// inherited from class Grandfather
   }
}
/*OUTPUT:
working...
sleeping... */

v) Multiple Inheritance:

When one class extends more than one class then this is called multiple inheritances. Java doesn’t allow multiple inheritances. Let's discuss why java doesn’t allow multiple inheritances and how we can use interfaces instead of classes to achieve the same purpose.

why java doesn't support multiple inheritance?

As you can see in the example flowchart the derived class (class Son) inherits all features of base classes (class Father  and class Mother). The problem occurs if both base classes (class Father and class Mother) have the same methods with the same signature. The compiler can’t determine which class method to be called. It creates ambiguity for the compiler, hence java doesn't support multiple inheritance.

Interface:

An Interface in Java programming is defined as an abstract type used to specify the behavior of a class. A Java interface contains static constants and abstract methods. An interface can implement multiple interfaces. In Java, interfaces are declared using the interface keyword.

Example:

import java.lang.*;
interface Father {
  public void work();
}
interface Mother {
  public void cook();
}
interface Son extends Father, Mother{
  public void play();
}
public class Inheritance{
  public static void main(String[] args){
     Son s = new Son() {
        public void work() {
           System.out.println("working...");
        }
        public void cook() {
           System.out.println("cooking...");
        }
        public void play() {
           System.out.println("playing...");
        }
     };

     s.work(); // inherited from base class 1
     s.cook(); // inherited from base class 2
     s.play(); // inherited from derived class
  }
}
/*OUTPUT:
working...
cooking...
playing... */