Skip to main content

Command Palette

Search for a command to run...

Inheritance in Java: Unlocking Code Reusability and Flexibility

Published
3 min read
Inheritance in Java: Unlocking Code Reusability and Flexibility
M

I am a backend developer, interested in writing about backend engineering, DevOps and tooling.

Introduction

Object-Oriented Programming (OOP) has been a cornerstone of modern software development, providing a structured and modular approach to coding. One of the key principles of OOP is inheritance, which allows developers to create new classes based on existing ones. Inheritance is not only about reusing code but also about establishing relationships between classes, which can lead to more flexible and maintainable software systems. In this blog, we'll delve into the concept of inheritance in Java, explore its usage, and walk through some code examples to demonstrate how it works in practice.

Usage

Inheritance in Java is used to achieve several objectives:

  • Code Reusability: By inheriting from existing classes, new classes can reuse fields and methods, reducing redundancy.

  • Method Overriding: Subclasses can override methods from the parent class to provide specific implementations.

  • Polymorphism: Inheritance supports polymorphism, where a subclass can be treated as an instance of its superclass, enabling more flexible and dynamic code.

  • Extensibility: New functionality can be added to existing classes without modifying them, making the system more extensible.

The keyword extends is used in Java to indicate that a class is inheriting from another class. The class that is inherited is called the superclass or parent class, and the class that inherits is known as the subclass or child class.

Code to Showcase

Let's look at a practical example to understand inheritance in Java.

// Superclass: Vehicle
class Vehicle {
    String brand;
    int year;

    public Vehicle(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }

    public void startEngine() {
        System.out.println("The engine is starting...");
    }

    public void stopEngine() {
        System.out.println("The engine is stopping...");
    }
}

// Subclass: Car
class Car extends Vehicle {
    int numberOfDoors;

    public Car(String brand, int year, int numberOfDoors) {
        super(brand, year); // Calling the constructor of the superclass
        this.numberOfDoors = numberOfDoors;
    }

    public void displayDetails() {
        System.out.println("Car brand: " + brand);
        System.out.println("Year of manufacture: " + year);
        System.out.println("Number of doors: " + numberOfDoors);
    }

    // Overriding the startEngine method
    @Override
    public void startEngine() {
        System.out.println("The car's engine is starting with a roar!");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Tesla", 2023, 4);
        myCar.startEngine(); // Calls the overridden method in Car
        myCar.displayDetails(); // Displays the details specific to Car
        myCar.stopEngine(); // Calls the method inherited from Vehicle
    }
}

Explanation:

  • Superclass (Vehicle): This class has two attributes, brand and year, and two methods, startEngine() and stopEngine().

  • Subclass (Car): This class extends Vehicle, inheriting its attributes and methods. It adds a new attribute numberOfDoors and a method displayDetails(). The startEngine() method is overridden to provide a more specific implementation for cars.

This demonstrates how the subclass Car can reuse and extend the functionality of the superclass Vehicle.

Conclusion

Inheritance is a powerful feature in Java that promotes code reuse and provides a framework for building hierarchical class structures. By understanding and utilizing inheritance, developers can create more organized, scalable, and maintainable code. The ability to override methods also allows for the creation of flexible systems where the same operation can behave differently depending on the context. Inheritance, coupled with other OOP principles like encapsulation and polymorphism, forms the foundation of robust Java applications.

More from this blog

Untitled Publication

30 posts