JavaAn Overview of Java Inheritance

An Overview of Java Inheritance

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java Programming tutorials

Inheritance is a mechanism by which a class can acquire the properties and behavior (represented by methods and fields/properties) of another class. It is a concept of object oriented programming (OOP) and is a prominent feature of the Java programming language.

This programming tutorial will discuss inheritance, its benefits, and how it can be implemented in Java.

You can learn more about object oriented programming in Java from the following tutorial: Classes and Objects in Java.

What is Inheritance in Java?

Inheritance is a key feature of object-oriented programming and is used extensively in the development of complex software systems. It facilitates code reuse, reduces code redundancy, and makes it easier to organize and maintain your code over time.

In addition to inheriting the non-private members of the base, the derived class can also add its own members or override the inherited members of the base class by providing its own implementation.

What is a Superclass in Java?

In object-oriented programming, a Superclass, also known as a parent class or base class, is a class that contains common properties and methods that are shared by its subclasses.

A Superclass can be thought of as a generalization of its subclasses, providing a common interface and behavior that can be shared among multiple classes. This is because changes made to the Superclass are automatically propagated to its subclasses.

In Java, the Object class is the ultimate Superclass of all classes, since all classes in Java inherit from it, either directly or indirectly.

What is a Subclass in Java?

In object-oriented programming, a subclass, also known as a derived class or child class, is a class that derives the properties and methods from a parent class, called a base or a Superclass, as discussed above.

The extends keyword in Java allows a subclass to inherit the public and protected properties and methods from its base or Superclass. To inherit a child class from its parent, you should specify the extends keyword, followed by the name of any base or Superclass from which it derives.

In addition to inheriting properties and methods from the Superclass, a subclass can also add its own methods and fields, providing additional functionality that is specific to the sub class.

By creating subclasses, developers can build on top of existing code and create new classes that are specialized for specific tasks, without having to rewrite code that already exists in the super or the base class.

How to Use the extends Keyword in Java

In Java, the extends keyword is used to create a derived class that inherits the properties and methods from its base. Here is the basic syntax for using the extends keyword in Java:

public class MySubclass extends MySuperclass 
{ 
   // Write your methods and fields here
}

In this example, MySubclass is the name of the subclass, and MySuperclass is the name of the Superclass that MySubclass is inheriting from. The extends keyword indicates that MySubclass is a subclass of MySuperclass. We will use the extends keyword in more detail in the section that follows.

What are the Benefits of Inheritance

Inheritance is an important concept in object-oriented programming and provides several benefits, including reusability, polymorphism, encapsulation, and easier code maintenance:

  • Code reuse: Inheritance allows developers to create a new class by extending an existing class, rather than creating a new class from scratch. This promotes code reuse and reduces redundancy, as common properties and methods can be inherited from a Superclass.
  • Polymorphism: Inheritance promotes designs that leverage polymorphism for improved flexibility and modularity in your software designs. By creating multiple subclasses that inherit from the same superclass, each subclass can have its own unique implementation of a method, while still being able to be treated as an object of the Superclass.
  • Encapsulation: Inheritance can also help to promote encapsulation, which is the practice of hiding implementation details from users. By defining the properties and methods of a Superclass, and only exposing those that are necessary, developers can create a clean, easy-to-use interface that hides the complexity of the underlying implementation.
  • Simplified maintenance: Inheritance can simplify the maintenance of a codebase by promoting a hierarchical structure. By organizing classes into a hierarchy, it becomes easier to modify, test, and debug code, as changes can be made to a Superclass, and these changes will be automatically inherited by all subclasses.
  • Improved productivity: Inheritance can also improve productivity by reducing the amount of code that needs to be written, and by making it easier to add new functionality to an existing codebase. This would foster faster development cycles, and faster release cycles as well.

You can learn more about polymorphism in our tutorial: What is Polymorphism in Java?

What are the Downsides of Inheritance in Java?

Despite its many benefits, inheritance has certain downsides as well, including tight coupling, more difficult readability, and code bloat:

  • Tight coupling: Inheritance introduces tight coupling between the base and its derived types, i.e., if you change one or more members of a base type, all types inheriting from the base type would be affected.
  • Inherited code bloat: Inheritance can lead to code bloat, where subclasses inherit properties and methods that they do not need, resulting in an unnecessarily large and complex codebase.
  • Fragile base classes: When a Superclass is changed in a way that inadvertently breaks the behavior of the subclasses inheriting from it, it is called the fragile base class problem. This can be especially problematic if the Superclass is widely used throughout a codebase, and can make it difficult to maintain and update the code.
  • Reduced readability: Inheritance can make code more difficult to read and understand, as it can introduce complex relationships between classes and make it more difficult to trace the flow of execution.

Read: The Best Tools for Remote Developers

Code Examples of Inheritance in Java

Here is a code example of how to use inheritance in Java:

class Vehicle {
    private int yearManufactured;
    private int numberOfYearsWarranty;
        public Vehicle(int yearManufactured, int numberOfYearsWarranty) {
        this.yearManufactured = yearManufactured;
        this.numberOfYearsWarranty = numberOfYearsWarranty;
    }
    public void move() {
        System.out.println("The vehicle is on the move...");
    }
    public int getYearOfManufacture() {
        return yearManufactured;
    }
    public int getWarrantyYears() {
        return numberOfYearsWarranty;
    }
}
 class Car extends Vehicle {
    private String model;
    public Car(int yearManufactured, int numberOfYearsWarranty, String model) {
        super(yearManufactured, numberOfYearsWarranty);
        this.model = model;
    }
    public String getModel() {
        return model;
    }
}
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car(2020, 7, "Honda Civic");
        System.out.println("Model: " + myCar.getModel());
        System.out.println("Year Manufactured: " + myCar.getYearOfManufacture());
        System.out.println("Warranty (in number of years): " + myCar.getWarrantyYears());
        myCar.move();
    }
}

In this example, Vehicle is a Superclass that defines a vehicle with a move() method. It also contains two variables that represent the year of manufacture and the years of warranty. Car is a subclass of Vehicle that adds a model and implements the move() method.

The Car constructor takes three parameters: the year of manufacture, the warranty in number of years, and the model of the car. It passes the first two parameters to the Superclass constructor using the super() method. The getModel() method returns the model name of the car.

When the program is run, it will output the following:

Model: Honda Civic
Year Manufactured: 2020
Warranty (in number of years): 7
The vehicle is on the move…

This Java code example demonstrates how inheritance can be used to create a more specialized class that inherits properties and methods from a more general class.

Java Inheritance Best Practices

Here are some best practices to keep in mind when using inheritance in Java:

  • Use composition over inheritance: In general, it is often better to favour composition over inheritance. Composition involves creating classes that contain instances of other classes, rather than extending existing classes. This approach can lead to more flexible and maintainable code, as it allows for greater modularity and encapsulation.
  • Use inheritance sparingly: It is important to use inheritance judiciously and only when it makes sense. Inheritance should be used to promote code reuse, simplify code maintenance, and enable polymorphism, not simply to reduce the amount of code that needs to be written.
  • Keep inheritance hierarchies shallow: Inheritance hierarchies should be kept as shallow as possible, with each subclass only inheriting from a single Superclass. This can help to reduce the risk of the fragile base class problem and make the codebase easier to maintain and understand.
  • Avoid using protected members: It is generally a good idea to avoid using protected members, as they can introduce tight coupling and make it difficult to change the behavior of a superclass without affecting its subclasses. Instead, use private members with getter and setter methods to allow subclasses to access the Superclass’s state.
  • Use abstract classes and interfaces to define contracts: Abstract classes and interfaces can be used to define contracts between classes, allowing for greater flexibility and modularity. You can take advantage of abstract classes to provide a partial implementation of a class. You can use both abstract classes and interfaces to define methods that an extending concrete type is forced to implement.
  • Document the inheritance hierarchy: It is important to document the inheritance hierarchy, including the relationship between classes and any restrictions on how they can be used. This would make your developers life easy and facilitate better code maintenance.

By following these best practices for inheritance in Java, developers can create code that is more maintainable, flexible, and extensible, while minimizing the potential pitfalls associated with inheritance.

Final Thoughts on Inheritance in Java

While inheritance can be a powerful tool for creating reusable, modular and extensible code, it is important to be mindful of these potential downsides and to use inheritance judiciously in order to create maintainable, flexible, and efficient code.

Read: Top Online Courses to Learn Java Development

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories