Inheritance in Java

7/23/2025

Inheritance in Java

Go Back

Inheritance in Java: Everything You Need to Know

Introduction

Inheritance is one of the four core concepts of Object-Oriented Programming (OOP) in Java. It allows a class to inherit properties and behaviors (fields and methods) from another class, promoting code reusability, scalability, and modularity.

In this article, we'll explore:

  • What inheritance is

  • Types of inheritance in Java

  • Syntax and implementation

  • Real-world examples

  • Best practices and rules


 Inheritance in Java

📖 What is Inheritance in Java?

Inheritance is the mechanism where one class (called the subclass or child class) derives the properties and behaviors of another class (called the superclass or parent class).

Java uses the extends keyword to implement inheritance:

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

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

✨ Output:

Dog d = new Dog();
d.eat(); // Inherited method

📒 Types of Inheritance in Java

1. Single Inheritance

One subclass inherits from one superclass.

class A {}
class B extends A {}

2. Multilevel Inheritance

A class inherits from a class which in turn inherits from another class.

class A {}
class B extends A {}
class C extends B {}

3. Hierarchical Inheritance

Multiple subclasses inherit from a single superclass.

class A {}
class B extends A {}
class C extends A {}

❌ Note:

Java does not support multiple inheritance (a class inheriting from more than one class) with classes to avoid ambiguity. However, it supports multiple inheritance with interfaces.


🔠 Superclass and Subclass

Superclass (Parent Class)

The class whose features are inherited.

Subclass (Child Class)

The class that inherits the features of another class.

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

class Bike extends Vehicle {
    void speed() {
        System.out.println("Bike speed is 80 km/hr");
    }
}

🪧 Real-Life Example of Inheritance

class Employee {
    String name;
    double salary;

    void displayInfo() {
        System.out.println("Name: " + name + ", Salary: " + salary);
    }
}

class Manager extends Employee {
    String department;

    void displayDept() {
        System.out.println("Department: " + department);
    }
}

public class Main {
    public static void main(String[] args) {
        Manager m = new Manager();
        m.name = "Alice";
        m.salary = 75000;
        m.department = "IT";
        m.displayInfo();
        m.displayDept();
    }
}

🔄 Method Overriding in Inheritance

A subclass can provide its own implementation of a method already defined in the parent class.

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

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

📌 Key Benefits of Inheritance

  • Promotes code reuse

  • Simplifies code maintenance

  • Enables method overriding and polymorphism

  • Improves readability and structure


💡 Best Practices

  • Use inheritance only when classes are logically related

  • Favor composition over inheritance if reuse isn't structural

  • Avoid deep inheritance chains (more than 2-3 levels)

  • Use super keyword to access superclass methods and constructors


🔎 FAQs: Inheritance in Java

Q1: Can a class extend more than one class in Java?
No. Java does not support multiple inheritance with classes.

Q2: What is the use of super keyword?
It refers to the parent class and can be used to access parent class members and constructors.

Q3: Can constructors be inherited?
No, but a subclass can call the constructor of its superclass using super().


🔗 Related Articles

Table of content