Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Object-Oriented Programming

Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented Programming system).

The idea behind inheritance in Java is that we can create new classes that are built upon existing classes. When we inherit from an existing class, we can reuse methods and fields of the parent class. Moreover, we can add new methods and fields in your current class as well.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Inheritance in Java.pngSubclass

The class that extends the features of another class is known as child class, subclass or derived class.

Superclass

The class whose properties and functionalities are used (inherited) by another class is known as parent class, superclass or Base class.

Advantages of Inheritance

  • Inheritance enables overriding, allowing the subclass to provide a meaningful implementation of a superclass method.

  • It promotes reusability by allowing the subclass to use the methods and fields defined in the parent class.

  • When changes are needed, updating the code in the superclass automatically reflects across all subclasses, simplifying maintenance.

  • Inheritance helps avoid code duplication by placing common logic in the superclass and sharing it across multiple classes.

Syntax

class Parent {
    void display() {
        System.out.println("This is the parent class.");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("This is the child class.");
    }
}

Here, Parent is the superclass, and Child is the subclass of the Parent class. The extends keyword indicates that we are creating a new class that inherits from an existing one.

public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
        obj.display(); // Inherited method
        obj.show();    // Child class method
    }
}

Inside the main method, when we create an object of the subclass, we can also access the methods inherited from the superclass.

This is the parent class.
This is the child class.

However, when we use a superclass reference to create an object, we can only access the methods defined in the superclass, not those unique to the subclass. This demonstrates the IS-A relationship, where Child IS-A Parent because it extends the Parent class.

public class Main {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.display(); // Inherited method
        obj.show();    // Child class method
    }
}

All the functionality of super class we can use in child class also.

Note: Private member of the class doesn’t participate in inheritance.

Types Of Inheritance

  • Single Level Inheritance

  • Multi Level Inheritance

  • Hierarchical Inheritance

  • Multiple Inheritance

  • Hybrid Inheritance

Note: Java does not support multiple or hybrid inheritance through classes because it can lead to the diamond problem, resulting in ambiguity.

Single Level Inheritance

image (21).pngMultilevel inheritance is a type of inheritance where one child class inherits the properties and behaviors of one parent class. It means there is only one parent and one child class.

Syntax

class parent_class_name {
    void eat() { }
}

class child_class_name extends parent_class_name {
    void bark() { }
}

Example

class A {
    void display() {
        System.out.println("This is class A (Parent class)");
    }
}

class B extends A {
    void show() {
        System.out.println("This is class B (Child class)");
    }
}

public class MasterBackend {
    public static void main(String[] args) {
        B obj = new B();
        obj.display(); 
        obj.show();    
    }
}

In this example, class A is the superclass, and class B is the subclass that inherits the properties and behaviors of the superclass. Since there is only one parent class and one child class, this demonstrates single-level inheritance.

Output

This is class A (Parent class)
This is class B (Child class)

Multi level Inheritance

image (22).pngMultilevel inheritance is a type of inheritance where a class inherits from a child class which is already inherited from another class. It means there is a chain of inheritance, one parent, one child, and one grandchild class.

Syntax

class parent_class_name {
    
}

class child_class_name extends parent_class_name {
    
}

class grand_child_class_name extends child_class_name {
    
}

Example

class A {
    void methodA() {
        System.out.println("This is class A (Grandparent)");
    }
}

class B extends A {
    void methodB() {
        System.out.println("This is class B (Parent)");
    }
}

class C extends B {
    void methodC() {
        System.out.println("This is class C (Child)");
    }
}

public class MasterBackend {
    public static void main(String[] args) {
        C obj = new C();
        obj.methodA(); 
        obj.methodB();         
        obj.methodC();   
        }
}

In this example, class A is the superclass, class B is a subclass of A, and class C is a subclass of B. As a result, class C inherits the properties and behaviors of both class A and class B. This is an example of multilevel inheritance in Java, where a class inherits from another class that is itself a subclass.

Output

This is class A (Grandparent)
This is class B (Parent)
This is class C (Child)

Hierarchical Inheritance

image (23).pngHierarchical inheritance is a type of inheritance where a single parent class is inherited by multiple child classes. In this structure, all child classes share the common properties and behaviors of the parent class, but they can also define their unique features.

Syntax

class parent_class_name {
    
}

class child_class_name_1 extends parent_class_name {
    
}

class child_class_name_2 extends parent_class_name {
    
}

Example

class A {
    void methodA() {
        System.out.println("This is class A (Parent)");
    }
}

class B extends A {
    void methodB() {
        System.out.println("This is class B (Child 1)");
    }
}

class C extends A {
    void methodC() {
        System.out.println("This is class C (Child 2)");
    }
}

public class MasterBackend {
    public static void main(String[] args) {
        B obj1 = new B();
        obj1.methodA(); // from A
        obj1.methodB(); // from B

        C obj2 = new C();
        obj2.methodA(); // from A
        obj2.methodC(); // from C
    }
}

In this example, class A is the superclass, while classes B and C are both subclasses of class A. This means that both B and C inherit all the properties and behaviors of class A. Since a single class is inherited by multiple subclasses, this represents hierarchical inheritance in Java—not multilevel inheritance.

Output

This is class A (Grandparent)
This is class B (Parent)
This is class C (Child)

Why is Multiple Inheritance not supported in Java through class?

image (24).pngHierarchical inheritance is a type of inheritance where a class can inherit properties of more than one parent class.

The problem occurs when there exist methods with same signature in both the super classes and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.

Multiple Inheritance is not permitted in java as it leads to diamond shaped problem which results in ambiguity.

Example

class A{
    void msg(){System.out.println("Hello");}
}
class B{
    void msg(){System.out.println("Welcome");}
}

public class C extends A, B{
    public static void main(String[] args) {
        C obj=new C();
        obj.msg();
    }
}

In this example, classes A and B are two parent classes, and class C is a child class and both the parent class have the same method msg(). If Java were to support multiple inheritance through classes, then when we call the method using an object of class C, the compiler would be confused about which version of the class method to be called, A or B and what is the priority of calling the methods. This situation leads to ambiguity, and to remove this problem java doesn’t support multiple inheritance with class in java.

Output

Error: java: ‘{‘ expected

What is Diamond shaped problem?

The Diamond Problem occurs in multiple inheritance when a class inherits from two classes that both inherit from the same parent class. This creates confusion because the compiler can't decide which version of a method or variable to use from the common parent. It forms a diamond shape in the class hierarchy, which is why it’s called the "Diamond Problem".

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board