What is inheritance in OOP (Java)?

Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows a class (called the child class or subclass) to inherit properties and behaviors (fields and methods) from another class (called the parent class or superclass). This promotes code reuse and establishes a natural relationship between classes.

What is inheritance in Java?

Inheritance is the mechanism in Java where one class acquires the properties (fields) and behaviors (methods) of another class. This allows the reuse of existing code and facilitates the addition of new features with minimal changes to the codebase.

Why do we need inheritance in Java?

  1. Code reusability: Reuse methods and variables from the parent class.

  2. Improved maintainability: Changes in the parent class reflect automatically in child classes.

  3. Real-world modeling: Models is-aAn "is-a relationship" in Java means one class inherits from another, like "a Car is-a Vehicle." relationships between objects.

  4. Polymorphism: Allows for method overriding to define specific behaviors in subclasses.

Consider a Vehicle as a superclass. It has properties like speed and fuelCapacity and methods like start() and stop(). A Car class can inherit from Vehicle and reuse these properties and methods while adding specific features like airConditioning or musicSystem.

Coding example for inheritance

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

The Vehicle class is the parent class, defining common attributes and methods such as speed, fuelCapacity, start(), and stop(). The Car class is a child class inheriting from Vehicle, adding its own unique attribute airConditioning and method playMusic(). The main method creates an instance of Car and uses both inherited and custom functionality.

Let’s explore the different ways inheritance can be implemented in Java.

Java inheritance types

Java supports several types of inheritance. Below are the types and their details:

1. Single inheritance

A single subclass inherits from one superclass. It models a simple "is-a" relationship where a subclass extends one parent class. For example, A Dog class inherits properties of an Animal class.

Example code

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

A child class (Dog) inherits from a single parent class (Animal). The Animal class defines a generic behavior (eat method), while the Dog class adds a specific behavior (bark method). The main method demonstrates how the Dog class can utilize both inherited and its own methods.

2. Multilevel inheritance

A class inherits from another class, which itself is inherited from a third class. It models a chain of inheritance, creating a hierarchical relationship. For example, A BabyDog class inherits from Dog, which in turn inherits from Animal.

A question arises that what happens if both parent and grandparent have the same function in multilevel inheritance. In multilevel inheritance, if both the parent and grandparent classes have a method with the same name and signature, the child class will inherit the method from the closest ancestor (the parent class).

Example Code

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

A class (BabyDog) inherits from a child class (Dog), which in turn inherits from a parent class (Animal). Each class contributes specific behaviors, and the BabyDog class can access methods from all its ancestor classes.

Try this: Add a new method to the Dog class (e.g., play()) and call it from the BabyDog class.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

3. Hierarchical inheritance

Multiple subclasses inherit from a single superclass. It promotes code reuse where multiple subclasses share common functionality from a parent class. For example, Cat and Dog classes both inherit from an Animal class.

Example Code

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

Multiple subclasses (Dog and Cat) inherit from a single parent class (Animal). Each subclass adds its own specific behaviors while inheriting the common behavior from the parent class.

4. Multiple inheritance

Java doesn’t support multiple inheritance directly using classes to avoid ambiguity. Instead, it achieves multiple inheritance using interfaces. It allows a class to implement multiple interfaces, enabling flexible designs. For example, A Robot can implement both Worker and Machine interfaces.

Example code

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

The Robot class implements two interfaces, Worker and Machine, providing specific implementations for their methods, doWork() and start(). The program shows how a single class can inherit behaviors from multiple sources, achieving flexibility and modularity.

5. Hybrid inheritance

Hybrid inheritance is a combination of two or more types of inheritance, like single and multiple inheritance. Java doesn’t support hybrid inheritance with classes to avoid complexity but achieves it through interfaces. For example, A class inherits from a superclass and also implements an interface.

Example code

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

The ElectricCar class extends the Vehicle class (inheriting its run() method) and implements the Electric interface (providing an implementation for the charge() method). This approach combines class inheritance with interface implementation to support multiple inheritance-like behavior.

Key takeaways

  1. Inheritance allows you to reuse existing code by extending a base class, reducing duplication and improving maintainability.

  2. Use inheritance to model "is-a" relationships, like Car is a Vehicle.

  3. Inheritance creates a clear class hierarchy, making code easier to understand and manage. Avoid overly deep inheritance trees to reduce complexity.

  4. Subclasses can override methods of the parent class, enabling dynamic method dispatch and more flexible code. This is essential for writing extensible and reusable code.

  5. Use appropriate access modifiers (protected, public, or private) to control visibility of members. Overuse of protected can lead to unintended access by subclasses.

  6. Overusing inheritance can lead to tightly coupled and harder-to-maintain code. Consider composition as an alternative when a class doesn't have a true "is-a" relationship.

  7. Java supports single inheritance to avoid the ambiguity of multiple inheritance (e.g., diamond problem). Use interfaces to simulate multiple inheritance if required.

  8. Inheritance can make debugging more challenging due to interactions between parent and child classes. Ensure proper testing of base and derived class functionalities.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Unlock your Java potential and take control of your coding journey! Start learning Java today to explore more than inheritance, build Java-based applications, and become a Java developer now!

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Attributions:
  1. undefined by undefined