DEV Community

Anees Abdul
Anees Abdul

Posted on • Edited on

Inheritance in Java:

What is Inheritance:

Inheritance is a mechanism where one class (called a child or subclass) acquires the properties and behaviors of another class (called a parent or superclass). This allows us to reuse code and create relationships between classes.

Why do we need Inheritance:

  1. Code Reusability: Instead of writing the same code over and over, you can write it once in a parent class and reuse it in multiple child classes.
  2. Organization: Inheritance helps organize your code in a logical, hierarchical way that mirrors real-world relationships.
  3. Easier Maintenance: When you need to make changes, you only update the parent class, and all child classes automatically inherit those changes.
  4. Extensibility: You can easily add new features to existing code without modifying it.

Types of Inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

1. Single Inheritance
One child class inherits from one parent class

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

class Child extends Parent {
    void display() {
        System.out.println("This is child class");
    }
}
public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
        obj.show();      // Inherited method
        obj.display();   // Child method
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Multiple Inheritance

Java does NOT support multiple inheritance with classes. Because, If both parent classes have the same method, compiler will not know which method need to be called. So, Java uses interfaces instead to achieve similar functionality.

3. MultiLevel Inheritance

A class inherits from a parent, and another class inherits from that child. A chain of inheritance (grandparent → parent → child).

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

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

class NewChild extends Child{
     void view(){
          System.out.println("This is a new child class");
    }
}

Enter fullscreen mode Exit fullscreen mode

4. Hierarchical Inheritance

Multiple child classes inherit from a single parent class.

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

class Child extends Parent {
    void display() {
        System.out.println("This is child class");
    }
}
class NewChild extends Parent{
     void view(){
          System.out.println("This is a new child class");
    }
}
public class Main {
    public static void main(String[] args) {
        NewChild obj = new NewChild();
        obj.show();      // Parent Method

    }
}

Enter fullscreen mode Exit fullscreen mode

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance (like single, multilevel, hierarchical, etc.).

Top comments (0)