Java Inheritance: Single, Multiple, and Hierarchical

10 Feb 2023
Intermediate
189 Views

Java Inheritance: Single, Multiple, and Hierarchical

Introduction

Inheritance is a mechanism that acquires one object to take all the properties and behavior from its parent class. Inheritance is one of the most important pillars of object-oriented programming.

What is Inheritance in Java?

Inheritance in Java assists to create a new class that can be built by taking help from an existing class. In this method, a class inherits from an existing one by reusing methods and fields from the class. Factors, that are important in Inheritance, are

  • Child or Subclass: it inherits the properties of a superior class
  • The parent or Superclass: the properties of this class inherited by the subclass

Syntax

class Subclass-name extends Superclass-name 
{ 
   //methods and fields 
} 

Types of Inheritance in Java

There are three types of Inheritance in Java

  1. Single Inheritance in Java
  2. Hierarchical Inheritance in Java
  3. Multilevel Inheritance in Java

Single Inheritance in Java

This is a single-level Inheritance with one subclass with the properties of the superclass.

Example

class Animal
    { 
    void sleep(){System.out.println("sleeping...");} 
    } 
    class Dog extends Animal
    { 
    void bark(){System.out.println("barking...");} 
    } 
    class TestInheritance
    { 
    public static void main(String args[])
    { 
    Dog d=new Dog(); 
    d.bark(); 
    d.sleep(); 
    }
    } 

Output

barking…

sleeping…

Multi-Level Inheritance in Java

If there are 3 classes named A, B, and C then class B inherits the properties of class A and class C inherits the properties of class B.

Example

class Animal
    { 
    void eat(){System.out.println("eating...");} 
    } 
    class Dog extends Animal{ 
    void sleep(){System.out.println("sleeping...");} 
    } 
    class BabyDog extends Dog
    { 
    void weep(){System.out.println("weeping...");} 
    } 
    class TestInheritance2
    { 
    public static void main(String args[])
    { 
    BabyDog d=new BabyDog(); 
    d.weep(); 
    d.sleep(); 
    d.eat(); 
    }
    }

Output

weeping…

sleeping…

eating…

Hierarchical Inheritance in Java

It multiplied one parent class with their child classes. This type of Inheritance in Java helps to reduce the code length and also increases the "code modularity".

Example

class Animal
    { 
    void eat(){System.out.println("eating...");} 
    } 
    class Dog extends Animal
    { 
    void sleep(){System.out.println("sleeping...");} 
    } 
    class Cat extends Animal
    { 
    void meow(){System.out.println("meowing...");} 
    } 
    class TestInheritance3
    { 
    public static void main(String args[])
    { 
    Cat c=new Cat(); 
    c.meow(); 
    c.eat(); 
    //c.bark();//C.T.Error 
    }
    } 

Output

meowing…

eating…

Summary

This article covers the idea if Inheritance in Java including its various types with examples. In Java, inheritance is when one class is able to inherit the attributes and methods of another. There are three types of inheritance in java- single, multilevel and hierarchical. While each type of inheritance has its own unique benefits, they all ultimately allow for code reusability which can make programs more efficient. 

Accept cookies & close this