● LIVE   Breaking News & Analysis
Alajir Stack
2026-05-02
Programming

Understanding Inheritance in Java: A Complete Guide

Learn Java inheritance: how subclasses inherit from superclasses using extends keyword. Explore single, multilevel, and hierarchical inheritance with code examples and best practices.

Introduction to Java Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors from another class. In Java, this mechanism promotes code reuse, method overriding, and a natural hierarchy among classes. By building relationships between classes, developers can create extensible and maintainable applications.

Understanding Inheritance in Java: A Complete Guide
Source: dev.to

This article covers the core aspects of inheritance in Java, including the extends keyword, types of inheritance, and practical examples to help you master this essential OOP feature.

Superclass and Subclass: The Parent-Child Relationship

In Java inheritance, you'll encounter two key terms:

  • Superclass (Parent class) – the class whose attributes and methods are inherited.
  • Subclass (Child class) – the class that inherits from the superclass.

The subclass can use all accessible members of the superclass (except those marked private) and can also define its own unique members. To establish this relationship, use the extends keyword.

Using the extends Keyword

The syntax for inheritance is simple: class Subclass extends Superclass { }. Here's a concrete example:

// Superclass
class Vehicle {
    String brand = "Ford";
    void honk() {
        System.out.println("Beep beep!");
    }
}

// Subclass inherits from Vehicle
class Car extends Vehicle {
    String modelName = "Mustang";
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.honk();          // inherited method
        System.out.println(myCar.brand + " " + myCar.modelName);
    }
}

In this example, Car inherits the honk() method and the brand field from Vehicle. The subclass can also access superclass methods directly, as shown.

Types of Inheritance in Java

Java supports several inheritance patterns, but it does not support multiple inheritance (a class inheriting from more than one class) to avoid ambiguity. The allowed types are:

1. Single Inheritance

In single inheritance, one subclass inherits from exactly one superclass. This is the most common form.

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

Here, Dog inherits eat() from Animal.

2. Multilevel Inheritance

Multilevel inheritance forms a chain: class A extends class B, and class B extends class C. Each subclass inherits from its immediate parent.

class A {
    void methodA() { System.out.println("Class A method"); }
}
class B extends A {
    void methodB() { System.out.println("Class B method"); }
}
class C extends B {
    void methodC() { System.out.println("Class C method"); }
}

Class C can invoke methods from A and B. This hierarchy models real-world relationships like Vehicle → Car → SportsCar.

3. Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses inherit from the same superclass. This is useful when several classes share common features defined in one parent.

Understanding Inheritance in Java: A Complete Guide
Source: dev.to
class Shape {
    void draw() { System.out.println("Drawing shape"); }
}
class Circle extends Shape {
    void area() { System.out.println("Area of circle"); }
}
class Rectangle extends Shape {
    void area() { System.out.println("Area of rectangle"); }
}

Both Circle and Rectangle reuse the draw() method from Shape.

Important Notes on Inheritance

  • Constructor chaining: When a subclass object is created, the superclass constructor is called first (implicitly or explicitly via super()).
  • Access modifiers: Only public and protected members are inherited. private members are not accessible directly, but they can be accessed via getter/setter methods if provided.
  • Method overriding: A subclass can provide a specific implementation of a superclass method (same signature). Use @Override annotation for clarity.
  • Final classes: A class declared with final cannot be inherited.
  • Multiple inheritance: Java does not allow a class to extend multiple classes. Instead, use interfaces to achieve multiple inheritance of type.

Benefits of Using Inheritance

  1. Code reusability: Write once in the superclass, reuse in all subclasses.
  2. Method overriding: Enables runtime polymorphism – a subclass can provide a specific behavior while keeping the same interface.
  3. Logical hierarchy: Models real-world relationships clearly (e.g., VehicleCar).
  4. Easier maintenance: Changes in a superclass propagate to subclasses automatically.

Conclusion

Java inheritance is a powerful tool for building modular and scalable applications. By understanding the extends keyword and the different inheritance types (single, multilevel, hierarchical), you can create clean, reusable class hierarchies. Remember to use access modifiers wisely and consider interfaces when you need the flexibility of multiple inheritance. Practice with the examples above to solidify your skills, and soon you'll harness the full potential of Java's OOP paradigm.