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:
Animalis the superclassDogis the subclass and usesextendsto inheritDoggets access toeat()method fromAnimal
Types of Inheritance in Java
| Type | Supported in Java? | Description |
|---|---|---|
| Single | Yes | One subclass inherits from one superclass |
| Multilevel | Yes | A class inherits from a subclass which inherits from another class |
| Hierarchical | Yes | Multiple 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
superis 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
superare 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?
extendsis used for inheriting classesimplementsis 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 :
