Javatpoint Logo
Javatpoint Logo

Multiple Inheritance in Java

Introduction

The concept of inheritance, which enables classes to adopt features and attributes from other classes, is fundamental to object-oriented programming. Due to Java's support for single inheritance, a class can only descend from one superclass. However, Java offers a method for achieving multiple inheritances through interfaces, enabling a class to implement many interfaces. We will examine the idea of multiple inheritance in Java, how it is implemented using interfaces, and use examples to help us understand.

Understanding Multiple Inheritance A class's capacity to inherit traits from several classes is referred to as multiple inheritances. This notion may be quite helpful when a class needs features from many sources. Multiple inheritances, however, can result in issues like the diamond problem, which occurs when two superclasses share the same method or field and causes conflicts. Java uses interfaces to implement multiple inheritances in order to prevent these conflicts.

Java interfaces

A Java interface is a group of abstract methods that specify the behavior that implementing classes must follow. It serves as a class blueprint by outlining each class's methods. Interfaces offer a degree of abstraction for specifying behaviors but cannot be instantiated like classes. In Java, a class can successfully implement several interfaces to achieve multiple inheritance.

Syntax of implementing multiple interfaces:

To implement multiple interfaces in Java, the following syntax is used:

The classes "MyClass" and "Interface1", "Interface2", and "Interface3" can now inherit and implement methods from other interfaces. As a result, the class is able to display the behaviours specified in every interface it implements.

Example - 1

Let's look at an example situation to demonstrate multiple inheritance using Java interfaces. Imagine that you and I are creating a game with a variety of characters, such as warriors and magicians. We also carry a variety of weaponry, including swords and wands. Although we want to keep character kinds and weapon types apart, we also want our characters to be able to utilize weapons. The following is how multiple inheritances through interfaces may help us do this:

Filename : MultipleInheritance.java

Output:

Warrior attacks with a sword.
Warrior uses a sword.
Mage attacks with a wand.
Mage uses a wand.

Explanation: The interfaces "Character" and "Weapon" in the example above specify the behaviour that classes that implement them must have. As a result of the classes "Warrior" and "Mage" implementing both interfaces, the necessary behaviors may be inherited and shown. The main method shows how to instantiate these classes' objects and call their corresponding behaviors.

Example - 2

Interfaces are not the only way to mimic multiple inheritance in Java; "Composition" or "Wrapper Classes" is another approach. This technique uses the target class to create objects of the appropriate classes, which are then assigned method calls. Let's examine this approach with an illustration:

Filename : MultipleInheritance2.java

Output:

Animal is eating.
Vehicle is being driven.

Explanation: Two classes, Animal and Vehicle, in the example above stand for two distinct behaviours. In order to integrate these behaviors, the class AnimalVehicle generates objects from both types. The AnimalVehicle class's eat() and drive() methods transfer respective method calls to the relevant Animal and Vehicle instances.

A kind of multiple inheritance where a class inherits behaviours from many sources can be accomplished by utilizing composition. With this method, we are able to merge the features of other classes without running into issues.

It's crucial to remember that, although offering a means of achieving multiple inheritance-like behaviors, the composition does not offer interfaces' degree of flexibility and modularity. Method calls must be manually delegated, which might be laborious in some situations.

Conclusion

Java offers interfaces as a strong substitute for the conventional multiple inheritance with classes that it does not provide. Classes can accomplish multiple inheritance and inherit behaviours from several sources by implementing multiple interfaces. This strategy encourages Java programmes' modularity and adaptability.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA