🧱 Java Object-Oriented Programming
Estimated reading: 4 minutes 48 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java Inheritance

Or Copy Link

CONTENTS
Scroll to Top