𧬠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 superclassDog
is the subclass and usesextends
to inheritDog
gets 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
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 classesimplements
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 :