What is Inheritance in Java: Types of Inheritance in Java

Inheritance is an OOPs concept that allows one class (subclass) to acquire the properties and behaviors (fields and methods) of another class (superclass) using the extends keyword.

Basic Example

class Animal {
    void eat() {
        System.out.println("This animal eats food");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks");
    }
}

Types of Inheritance in Java

  • Single Inheritance — one subclass inherits from one superclass
  • Multilevel Inheritance — a class inherits from a class which itself inherits from another class
  • Hierarchical Inheritance — multiple subclasses inherit from the same superclass
  • Multiple Inheritance — a class inherits from more than one class (not supported directly with classes in Java, only via interfaces)
  • Hybrid Inheritance — a combination of two or more types of inheritance, achieved in Java using interfaces

Why Use Inheritance?

  • Promotes code reuse by letting subclasses reuse fields and methods from a parent class
  • Supports method overriding, enabling runtime polymorphism
  • Helps organize related classes into a logical hierarchy
Java does not support multiple inheritance with classes to avoid the diamond problem, but it achieves similar flexibility through interfaces.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course