Home » Java programming language

Types of Inheritance in Java with Examples

In this tutorial, we are going to learn about the types of Inheritance in Java: Here we will discuss Single, Multiple, Multilevel, and Hierarchical Inheritance in Java with Examples.
Submitted by Preeti Jain, on June 02, 2019

Prerequisite: Inheritance and its implementation in Java

Type of inheritance in Java

In Java programming, there are following types of the inheritances,

  1. Single Inheritance
  2. Multiple Inheritances (Through Interface)
  3. Multilevel Inheritance
  4. Hierarchical Inheritance

1) Single Inheritance

If a class extends another class (i.e. the only one class).

Syntax:

    class Parent {
        // Fields and Methods
    }

    class Child extends Parent {
        // Fields and Methods
    }

Example of Single Inheritance:

/*Java program to demonstrate the  
 concept of inheritance */

// Parent class 
class Parent {
    // The Parent class has one method
    // displayParentMessage() method to print message of Parent Class
    public void displayParentMessage() {
        System.out.println("Hello, we are in parent class method");
    }
}

// Sub class or derived class
class Child extends Parent {
    // The Child subclass adds one more method
    // displayChildMessage() method to print message of Parent Class
    public void displayChildMessage() {
        System.out.println("Hello, we are in child class method");
    }
}

// Main class in this class we will create 
//object of parent and child class 
class Main {
    public static void main(String[] args) {

        // Creation of Parent class object
        Parent p = new Parent();

        // Calling Parent class method by Parent class object
        p.displayParentMessage();

        // Creation of Child class object
        Child c = new Child();

        // Calling Child class method by Child class object
        c.displayChildMessage();

        // Calling Parent class method by Child class object
        c.displayParentMessage();
    }
}

Output

D:\Programs>javac Main.java
D:\Programs>java Main
Hello, we are in parent class method
Hello, we are in child class method
Hello, we are in parent class method

2) Multiple Inheritance (Through Interface)

If we extend more than one class. Java doesn’t support multiple inheritances directly but with the help of interface we can implement but it is similar to multiple inheritance.

Syntax:

    interface interface1 {
        // Field and Method declaration
    }
    interface interface2 {
        // Field and Method declaration
    }
    Class class_name implements interface1, interface2 {}

Example of Multiple Inheritance:

/*Java program to demonstrate the  
 concept of multiple inheritance */

interface Print {
    void print();
}
interface Show {
    void show();
}

class Main implements Print, Show {
    public void print() {
        System.out.println("Hello");
    }
    public void show() {
        System.out.println("World");
    }

    public static void main(String args[]) {
        Main obj = new Main();
        obj.print();
        obj.show();
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
Hello
World

3) Multilevel Inheritance

If a classA extends by classB and classB extends by classC is called multilevel inheritance.

Syntax:

    class classA {
        // Fields and Methods
    }

    class classB extends classA {
        // Fields and Methods
    }
    class classC extends classB {
        // Fields and Methods
    }

Example of Multilevel Inheritance:

/*Java program to demonstrate the  
 concept of multilevel inheritance */

// ClassA class 
class ClassA {
    // The ClassA class has one method
    // displayClassAMessage() method to print message of ClassA Class
    public void displayClassAMessage() {
        System.out.println("Hello, we are in ClassA class method");
    }
}

// ClassB class
class ClassB extends ClassA {
    // The ClassB class adds one more method
    // displayClassBMessage() method to print message of ClassB Class
    public void displayClassBMessage() {
        System.out.println("Hello, we are in ClassB class method");
    }
}

// ClassC class
class ClassC extends ClassB {
    // The ClassC class adds one more method
    // displayClassCMessage() method to print message of ClassC Class
    public void displayClassCMessage() {
        System.out.println("Hello, we are in ClassC class method");
    }
}

// Main class in this class we will create 
//object of ClassA and ClassB and ClassC class 
class Main {
    public static void main(String[] args) {

        // Creation of ClassA class object
        ClassA ca = new ClassA();

        // Calling ClassA class method by ClassA class object
        ca.displayClassAMessage();

        // Creation of ClassB class object
        ClassB cb = new ClassB();

        // Calling ClassB class method by ClassB class object
        cb.displayClassBMessage();

        // Calling ClassA class method by ClassB class object
        cb.displayClassAMessage();

        // Creation of ClassC class object
        ClassC cc = new ClassC();

        // Calling ClassC class method by ClassC class object
        cc.displayClassCMessage();

        // Calling ClassB class method by ClassC class object
        cc.displayClassBMessage();

        // Calling ClassA class method by ClassC class object
        cc.displayClassAMessage();

    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
Hello, we are in ClassA class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method
Hello, we are in ClassC class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method

4) Hierarchical Inheritance

If more than one class is inherited from the base class is called hierarchical inheritance.

Syntax:

    class classA {
        // Fields and Methods
    }

    class classB extends classA {
        // Fields and Methods
    }
    class classC extends classA {
        // Fields and Methods
    }

Example of Hierarchical Inheritance:

/*Java program to demonstrate the  
concept of hierarchical inheritance */

//ClassA
class ClassA {
    // The ClassA class has one method
    // displayClassAMessage() method to print message of ClassA Class
    public void displayClassAMessage() {
        System.out.println("Hello, we are in ClassA class method");
    }
}


// ClassB class
class ClassB extends ClassA {
    // The ClassB class adds one more method
    // displayClassBMessage() method to print message of ClassB Class
    public void displayClassBMessage() {
        System.out.println("Hello, we are in ClassB class method");
    }
}

// ClassC class
class ClassC extends ClassA {
    // The ClassC class adds one more method
    // displayClassCMessage() method to print message of ClassC Class
    public void displayClassCMessage() {
        System.out.println("Hello, we are in ClassC class method");
    }
}

// Main class in this class we will create 
//object of ClassA and ClassB and ClassC class 
class Main {
    public static void main(String[] args) {

        // Creation of ClassA class object
        ClassA ca = new ClassA();

        // Calling ClassA class method by ClassA class object
        ca.displayClassAMessage();

        // Creation of ClassB class object
        ClassB cb = new ClassB();

        // Calling ClassB class method by ClassB class object
        cb.displayClassBMessage();

        // Calling ClassA class method by ClassB class object
        cb.displayClassAMessage();

        // Creation of ClassC class object
        ClassC cc = new ClassC();

        // Calling ClassC class method by ClassC class object
        cc.displayClassCMessage();

        // Calling ClassA class method by ClassC class object
        cc.displayClassAMessage();

    }
}

Output

D:\Programs>javac Main1.java

D:\Programs>java Main1
Hello, we are in ClassA class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method
Hello, we are in ClassC class method
Hello, we are in ClassA class method


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.