🧱 Java Object-Oriented Programming
Estimated reading: 4 minutes 518 views

Java Inheritance – A Complete Guide with Syntax, Examples & Best Practices


Introduction – Why Inheritance Is Essential in Java

Imagine writing duplicate code for common behavior in every class β€” sounds painful, right? Java inheritance solves this by letting you define properties and behaviors once and reuse them across multiple classes.

Java inheritance is a fundamental concept in Object-Oriented Programming (OOP) that promotes code reusability, scalability, and maintainability.

By the end of this guide, you’ll learn:

What inheritance is and how it works in Java
The different types of inheritance in Java
Real-world examples and syntax
Best practices and limitations


What is Inheritance in Java?

Inheritance allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass).

This enables:

  • Code reuse
  • Logical hierarchy
  • Runtime polymorphism

Basic Syntax of Inheritance

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

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

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();  // inherited
        d.bark(); // own method
    }
}

Explanation:

  • Animal is the superclass
  • Dog is the subclass and uses extends to inherit
  • Dog gets access to eat() method from Animal

Types of Inheritance in Java

TypeSupported in Java?Description
Single YesOne subclass inherits from one superclass
Multilevel YesA class inherits from a subclass which inherits from another class
Hierarchical YesMultiple subclasses inherit from a single superclass
Multiple No (via class)Not supported directly to avoid ambiguity (can be done via interfaces)
Hybrid No (via class)Combination of two or more types; partially possible with interfaces

Single Inheritance

class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Car extends Vehicle {
    void horn() {
        System.out.println("Car horn sounds");
    }
}

One subclass inherits from one superclass


Multilevel Inheritance

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

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

class Puppy extends Dog {
    void weep() {
        System.out.println("Weeping...");
    }
}

Puppy inherits from Dog, which inherits from Animal


Hierarchical Inheritance

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

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

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

Multiple classes (Dog, Cat) inherit from a single class (Animal)


Multiple Inheritance Issue (Diamond Problem)

interface A {
    void show();
}

interface B {
    void show();
}

class C implements A, B {
    public void show() {
        System.out.println("Hello from C");
    }
}

Java avoids multiple inheritance via classes
Java supports multiple inheritance through interfaces only


super Keyword in Inheritance

  • super is used to:
    • Call the superclass constructor
    • Access superclass methods or fields
class Animal {
    Animal() {
        System.out.println("Animal is created");
    }
}

class Dog extends Animal {
    Dog() {
        super(); // optional if default constructor
        System.out.println("Dog is created");
    }
}

Method Overriding – Core of Inheritance

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

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

Method overriding provides runtime polymorphism
Enables custom behavior in child classes


Best Practices for Java Inheritance

Use inheritance for β€œis-a” relationships only
Prefer composition over inheritance when possible
Avoid deep inheritance hierarchies β€” they’re harder to maintain
Use @Override annotation to ensure accurate overriding
Use super() for constructor chaining and method access


Summary

  • Inheritance enables one class to acquire fields and methods from another
  • Java supports single, multilevel, and hierarchical inheritance
  • Method overriding and use of super are key to leveraging inheritance
  • Use inheritance wisely to avoid complex code and improve modularity

FAQs – Java Inheritance

What is the main use of inheritance in Java?

To achieve code reusability, logical hierarchy, and polymorphic behavior.

Can constructors be inherited?

No, constructors are not inherited, but the superclass constructor is invoked during subclass creation.

Is multiple inheritance possible in Java?

Not with classes, but it is possible using interfaces.

What’s the difference between extends and implements?

  • extends is used for inheriting classes
  • implements is used to inherit interfaces

When should I avoid inheritance?

Avoid when:

  • There is no β€œis-a” relationship
  • Behavior should not be shared
  • Using composition is more modular and testable

Share Now :
Share

Java Inheritance

Or Copy Link

CONTENTS
Scroll to Top