Inheritance in Object-Oriented Programming with Java.

Kavindu Sandaruwan
5 min readJan 9, 2024

--

Inheritance is one of the fundamental concepts of object-oriented programming (OOP) that allows us to create new classes from existing ones. In this blog post, we will learn what inheritance is, why it is useful, how to use it in Java, and what types of inheritance are supported in Java.

What is Inheritance?

Inheritance is a mechanism that enables a class to inherit the features (fields and methods) of another class. The class that inherits the features is called the subclass or the child class, and the class that provides the features is called the superclass or the parent class.

Inheritance allows us to reuse the code that is already written in the superclass and extend or modify it in the subclass. This way, we can avoid code duplication and achieve code reusability.

Inheritance also establishes a relationship between classes. We can say that a subclass is-a type of superclass. For example, a Dog class can inherit from an Animal class, and we can say that a dog is an animal.

Why is Inheritance Useful?

Inheritance is useful for several reasons, such as:

- It promotes code reusability by allowing us to reuse the fields and methods of the superclass in the subclass.
- It supports method overriding, which is a technique that allows us to redefine the behavior of a method in the subclass. This enables us to achieve polymorphism, which is the ability of an object to take different forms depending on the context. We will discuss Polymorphism in detail in the next chapter.
- It facilitates code maintenance by making the code more organized and modular. We can group the common features in the superclass and the specific features in the subclass.
- It enhances code readability by making the code more understandable and logical. We can easily see the hierarchy and the relationship between classes.

How to Use Inheritance in Java?

In Java, we use the extends keyword to perform inheritance. The syntax is:

The extends keyword indicates that the subclass inherits the features of the superclass. The subclass can access the public and protected fields and methods of the superclass, as well as the default package fields and methods if they are in the same package. The subclass cannot access the private fields and methods of the superclass.

The subclass can also add its own fields and methods, or override the methods of the superclass. To override a method, we use the @Override annotation and provide a new implementation of the method in the subclass.

Here is an example of inheritance in Java:

Super class
Sub class or child class
Main class

The output of the above code is:
Max is eating.
Dog food is delicious.
Max is sleeping.
Woof woof!

In the above example, we have created a superclass Animal and a subclass Dog. The Dog class inherits the fields and methods of the Animal class using the extends keyword. The Dog class also adds a new field breed and a new method bark. The Dog class also overrides the eat method of the Animal class and provides a new implementation.

We have also created an object of the Dog class and called the methods of the subclass and the superclass. Notice how we can access the eat and sleep methods of the Animal class using the object of the Dog class. This is because the Dog class inherits these methods from the Animal class. We can also access the bark method of the Dog class using the same object. This is because the Dog class adds this method to its own features.

What Types of Inheritance are Supported in Java?

Inheritance can be classified into different types based on the number and the relationship of the classes involved. Some of the common types of inheritance are:

- Single inheritance: This is the simplest form of inheritance, where a subclass inherits from only one superclass. For example, Dog inherits from Animal.
- Multilevel inheritance: This is a form of inheritance where a subclass inherits from another subclass, which in turn inherits from another superclass. For example, Puppy inherits from Dog, which inherits from Animal.
- Hierarchical inheritance: This is a form of inheritance where more than one subclass inherits from a single superclass. For example, Dog and Cat both inherit from Animal.
- Multiple inheritance: This is a form of inheritance where a subclass inherits from more than one superclass. For example, Bat inherits from Animal and Bird. However, this type of inheritance is not supported in Java using classes, because it can lead to ambiguity and complexity. Java supports multiple inheritance using interfaces, which are a different concept that we will discuss later.

Here is a diagram that illustrates the different types of inheritance:

Sakpal, 2018

Conclusion

In this blog post, we have learned about inheritance in object-oriented programming with Java. We have seen what inheritance is, why it is useful, how to use it in Java, and what types of inheritance are supported in Java. We have also seen some examples of inheritance in Java using classes.

Inheritance is a powerful concept that allows us to reuse, extend, and modify the code of existing classes. It also helps us to establish relationships and hierarchies between classes. Inheritance is one of the core features of OOP that makes the code more organized, modular, readable, and maintainable.

I hope this blog post has helped you understand inheritance in object-oriented programming with Java. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading. 😊

--

--