Paramclasses Academy - Blog

Tutorials Details

What is Inheritance in Java: Types and Practical Examples ?

What is Inheritance in Java: Types and Practical Examples ?

October 21, 2026

Introduction

Inheritance is one of the four commonly taught pillars of Object-Oriented Programming (OOP) in Java. It allows a class to inherit accessible fields and methods from another class, helping developers reuse and extend existing code.

For example, a Dog is an Animal. Both may share behaviors such as eating, while a dog can also have its own behavior, such as barking.

In Java, inheritance is commonly implemented using the extends keyword for classes. In this tutorial, you will learn what inheritance is, its main types, the use of super, method overriding, and practical examples.

What Is Inheritance in Java?

Inheritance is an OOP mechanism in which a class derives from another class and inherits its accessible members.

The existing class is called the parent class, base class, or superclass. The new class is called the child class, derived class, or subclass.

Real-Life Example

Imagine a general class called Vehicle. It might define common properties and behaviors, such as a brand and a method to start the vehicle.

Classes such as Car and Bike can extend Vehicle and add their own specific behavior.

This allows shared functionality to be defined once and reused where the relationship makes sense.

Syntax of Inheritance in Java

Java uses the extends keyword to inherit from a class.

class Parent {
    void display() {
        System.out.println("Parent class method");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("Child class method");
    }
}

Here, Child extends Parent. A Child object can call both display() and show() because the inherited method is accessible.

Simple Inheritance Example

Let's create a parent class called Animal and a child class called Dog.

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();

        dog.eat();
        dog.bark();
    }
}

Output:

Animal is eating
Dog is barking

Explanation

  1. Animal is the parent class.

  2. Dog extends Animal declares inheritance.

  3. eat() is defined in the parent class.

  4. bark() is defined in the child class.

  5. The Dog object can call both methods.

This demonstrates how a child class can reuse behavior from its parent class and provide additional functionality.

Types of Inheritance in Java

The common class-inheritance structures are:

  1. Single inheritance

  2. Multilevel inheritance

  3. Hierarchical inheritance

Java classes do not support multiple inheritance from multiple classes. However, Java supports multiple inheritance of type through interfaces, which a class can implement together.

Let's understand each type.

1. Single Inheritance

Single inheritance occurs when one child class inherits from one parent class.

<text color="secondary" size="sm">Structure: Parent → Child</text>

Example

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();

        dog.eat();
        dog.bark();
    }
}

Output:

Eating
Barking

In this example, Dog directly extends Animal. This is single inheritance.

2. Multilevel Inheritance

Multilevel inheritance occurs when a class extends another class that itself extends a third class.

<text color="secondary" size="sm">Structure: Grandparent → Parent → Child</text>

Example

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

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

class Puppy extends Dog {
    void play() {
        System.out.println("Playing");
    }
}

public class Main {
    public static void main(String[] args) {
        Puppy puppy = new Puppy();

        puppy.eat();
        puppy.bark();
        puppy.play();
    }
}

Output:

Eating
Barking
Playing

Explanation

  • Dog extends Animal.

  • Puppy extends Dog.

  • A Puppy object can access the inherited accessible methods from both classes.

This is multilevel inheritance because the inheritance chain contains multiple levels.

3. Hierarchical Inheritance

Hierarchical inheritance occurs when multiple child classes inherit from the same parent class.

<text color="secondary" size="sm">Structure: Parent → Child A and Child B</text>

Example

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Cat is meowing");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();

        dog.eat();
        dog.bark();

        cat.eat();
        cat.meow();
    }
}

Output:

Animal is eating
Dog is barking
Animal is eating
Cat is meowing

Both Dog and Cat inherit from Animal, but each also defines its own behavior.

Does Java Support Multiple Inheritance?

Java does not allow a class to extend two classes directly.

The following is invalid Java:

// Invalid Java — a class cannot extend two classes
class Child extends ParentA, ParentB {
}

This restriction avoids ambiguity that can arise when two parent classes provide conflicting implementations.

However, a Java class can implement multiple interfaces.

Example Using Multiple Interfaces

interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Document implements Printable, Showable {
    public void print() {
        System.out.println("Printing document");
    }

    public void show() {
        System.out.println("Showing document");
    }
}

public class Main {
    public static void main(String[] args) {
        Document document = new Document();

        document.print();
        document.show();
    }
}

Output:

Printing document
Showing document

The Document class implements two interfaces and provides implementations for both methods.

The super Keyword in Java

The super keyword refers to the parent-class portion of the current object. It is commonly used to:

  • Call a parent-class constructor using super().

  • Access an accessible parent-class field hidden by a child field.

  • Call an overridden parent-class method.

Example: Calling a Parent Method

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        super.sound();
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound();
    }
}

Output:

Animal makes a sound
Dog barks

Explanation

  • Dog overrides the sound() method.

  • super.sound() calls the parent class's implementation.

  • The child method then prints its own message.

This is useful when a child class wants to extend the parent's behavior rather than replace it completely.

Constructor and Inheritance

Constructors are not inherited by subclasses. When creating a child object, Java also initializes its parent-class portion.

If a parent class has a suitable no-argument constructor, Java can invoke it implicitly. If the parent only provides parameterized constructors, the child constructor generally needs an explicit super(arguments) call.

Example

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

class Student extends Person {
    int rollNumber;

    Student(String name, int rollNumber) {
        super(name);
        this.rollNumber = rollNumber;
    }

    void display() {
        System.out.println(name + " - " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student("Aman", 101);
        student.display();
    }
}

Output:

Aman - 101

Here, super(name) initializes the name field through the Person constructor. The Student constructor initializes rollNumber.

Method Overriding in Inheritance

Method overriding occurs when a subclass provides its own implementation of an inherited instance method, following Java's overriding rules.

Example

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound();
    }
}

Output:

Dog barks

Even though the reference type is Animal, the object is a Dog. Java selects the overridden instance method at runtime.

This behavior is also known as runtime polymorphism.

Advantages of Inheritance in Java

  1. Code reuse: A subclass can reuse accessible behavior from its superclass.

  2. Extensibility: A subclass can add new fields and methods.

  3. Method overriding: Subclasses can specialize inherited behavior.

  4. Polymorphism: Different subclasses can be used through a common parent type.

  5. Organized design: Shared behavior can be grouped in a suitable base class.

Disadvantages of Inheritance

Inheritance is useful, but it is not always the best design choice.

  • Tight coupling: A subclass can depend heavily on its parent class's design.

  • Complex hierarchies: Deep inheritance chains can make code harder to understand.

  • Limited flexibility: Java classes can directly extend only one class.

  • Fragile base class risk: Changes in a parent class may unintentionally affect subclasses.

Use inheritance when a genuine “is-a” relationship exists. For a “has-a” relationship, composition—where one object contains or uses another object—may be more suitable.

Inheritance vs Composition

Inheritance Composition
Represents an “is-a” relationship Represents a “has-a” relationship
Uses extends for class inheritance Uses object references and fields
Subclass inherits accessible behavior One object uses another object's functionality
Can create tight coupling between parent and child Often allows more flexible component replacement

For example, a Dog is an Animal, so inheritance may make sense. A Car has an Engine, so composition is generally a natural design.

Common Mistakes Beginners Should Avoid

  1. Forgetting the extends keyword when declaring a subclass.

  2. Assuming Java classes can extend multiple classes.

  3. Trying to access a parent's private fields directly from a subclass.

  4. Assuming constructors are inherited.

  5. Confusing method overloading with method overriding.

  6. Using inheritance when composition better represents the relationship.

  7. Forgetting to use @Override when overriding a method; the annotation helps the compiler detect mistakes.

Frequently Asked Questions (FAQs)

1. What is inheritance in Java?

Inheritance is an OOP mechanism that lets a class derive from another class and reuse its accessible fields and methods.

2. What are the main types of inheritance in Java?

The common class-inheritance structures are single, multilevel, and hierarchical inheritance.

3. Does Java support multiple inheritance?

Java classes cannot extend multiple classes. However, a class can implement multiple interfaces.

4. What is the extends keyword?

The extends keyword declares that one class inherits from another class.

5. What is the super keyword?

The super keyword is used to invoke a parent constructor or access an accessible parent member, such as an overridden method.

6. Are constructors inherited in Java?

No. Constructors are not inherited. A subclass constructor can invoke a parent constructor using super() or super(arguments).

7. What is method overriding?

Method overriding occurs when a subclass provides its own implementation of an inherited instance method, enabling runtime polymorphism.

8. What is the difference between inheritance and polymorphism?

Inheritance establishes a relationship between classes and enables reuse. Polymorphism allows the same common reference or method contract to represent different implementations.

Practice Exercises for Beginners

Try these exercises to strengthen your understanding:

  1. Create a Vehicle parent class and a Car child class.

  2. Create a multilevel hierarchy: Person → Employee → Manager.

  3. Create a hierarchical inheritance example with Animal, Dog, and Cat.

  4. Create a parent class with a parameterized constructor and invoke it from a child constructor using super().

  5. Override a display() method in a child class and call it through a parent-class reference.

  6. Create two interfaces and implement both in one class.

Conclusion

Inheritance is a powerful Java OOP feature that helps programmers reuse and extend existing code. The extends keyword establishes class inheritance, while super provides access to parent constructors and accessible members.

Understanding single, multilevel, and hierarchical inheritance will help you build a strong foundation for method overriding and polymorphism. Always choose inheritance when the class relationship makes sense, and consider composition when one object simply uses another.