Programming

Understanding Inheritance in Java: A Complete Guide

2026-05-02 06:21:44

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:

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

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.

Explore

Securing Cargo: A Practical Guide to the tar Crate Vulnerability (CVE-2026-33056) Deep Dive: Cricut’s Joy 2 makes creating stickers easier for beginners, and... Mastering Human Agency in an AI-Driven World: A Practical Guide How Huawei is Poised to Dominate China's AI Chip Market by 2026: A Comprehensive Guide Python Security Response Team: New Governance and Growing Community Enhance Ecosystem Safety