Last Updated: January 3, 2026
Inheritance is a core concept in object-oriented programming that allows one class to inherit the properties and behaviors of another.
This mechanism not only promotes code reusability but also establishes a natural hierarchy among classes. If you've ever wondered how we can create a structure that reflects real-world relationships or how we can avoid redundancy in our code, inheritance is the answer.
In this chapter, we'll dig into the basics of inheritance in Java. We’ll explore how it works, why it’s important, and provide real-world examples to illustrate its utility.
At its core, inheritance is a way for one class to derive properties and methods from another class. The class that inherits is called the subclass (or child class), while the class being inherited from is the superclass (or parent class). This relationship allows subclasses to reuse code from superclasses, which can make your programs easier to maintain and extend.
Let’s start with a straightforward example to illustrate inheritance. Consider a basic structure for animals.
In this example, both Dog and Cat classes extend the Animal class. They inherit the eat method, which represents a common behavior for all animals. Each subclass can also define its unique methods, such as bark for Dog and meow for Cat.
Java supports single inheritance, meaning a class can only inherit from one superclass. This design choice helps to avoid issues like the "Diamond Problem," which can occur in languages that support multiple inheritance.
You can visualize the inheritance hierarchy as a tree structure. In our previous example, the hierarchy looks like this:
extends KeywordIn Java, we use the extends keyword to indicate that a class is inheriting from another class. The subclass automatically gets access to all public and protected members of its superclass. This creates a clear and organized way to structure your classes.
Here’s a more detailed example that shows the concept of inheritance in a slightly different context:
This example demonstrates how both Car and Bike inherit the start method from the Vehicle superclass, allowing for a clear understanding of shared functionality among vehicles.
When it comes to inheritance, access modifiers play a crucial role in determining what members of the superclass are accessible in the subclass. Here’s a quick breakdown:
Let’s expand on our previous example to see how these modifiers work in practice:
In this code, the Manager class can access the name and id fields but cannot access the salary field, demonstrating how access levels affect inheritance.
Inheritance can be particularly useful in scenarios where you have a group of related classes. For example, consider an application for managing different types of accounts in a banking system. You can create a base class, Account, and then derive specific account types like SavingsAccount and CheckingAccount.
In this example, both the SavingsAccount and CheckingAccount classes inherit from the Account superclass. They can use the shared deposit and displayBalance methods while also implementing their unique functionalities.
As you start working with inheritance, here are some best practices to keep in mind:
By understanding these principles, you can effectively design your Java applications to take full advantage of inheritance while maintaining clarity and ease of maintenance.
Now that you understand the basics of inheritance, you are ready to explore the extends keyword in the next chapter.
This will deepen your understanding of how to create subclasses and leverage inherited functionality more effectively.