Why Multiple Inheritance is not Supported in Java?27 Mar 2025 | 6 min read In Java, OOP features enable classes to adopt features and attributes from other classes. In this section, we will discuss multiple inheritance in Java and also discuss why Java does not support multiple inheritance. What is Multiple Inheritance?A class that inherits behaviors and functions from several parent classes is referred to as multiple inheritances. Multiple inheritance is supported by many object-oriented programming languages, such as C++, but Java does not support it. Complexity management, ambiguity resolution, and code management issues are some of the factors that led to this design decision. Why Java does not support multiple inheritance?Design philosophy is the major reason for not supporting multiple inheritance in Java. Design philosophy prioritizes clarity and simplicity over the complexity of code. To overcome this confusion and clarity of code, Java decided to forbid multiple inheritance. In single inheritance, a child class inherits from one parent class only, which is the immediate superclass. It guarantees a clear hierarchical structure. It can also make code maintenance simpler and less likely to result in conflicts. But in the case of multiple inheritance, not so. Example of Multiple InheritanceConsider the following figure for a better understanding. ![]() In the above image, we have taken one child class, two-parent classes (Parent1 and Parent2), and a base class. Both parent classes and base classes have a method named print(). The child class inherits from two parent classes (Parent1 and Parent2), and both parent classes inherit from a common base class. Both the parent classes override a method (print()) from the base class. In this scenario, ambiguity arises when the child class tries to inherit the print() method. Since both parent classes provide their own implementations of the print() method. So, there is an ambiguity that which version the child class should inherit if it does not offer its own override. This ambiguity makes the code harder to understand, maintain, and debug. This scenario is avoided in Java by not supporting multiple inheritance of classes. Let’s see how to overcome this problem. How to achieve multiple inheritance in Java?Java supports multiple inheritance through interfaces only, where a class can implement multiple interfaces. Multiple inheritance in Java is not possible by class, but it is possible through interfaces, where a class can implement multiple interfaces. The interface allows classes to inherit methods and functionality from various classes or sources. Using InterfaceA 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: Multiple Inheritance Java Program Using Interface ExampleCompile and RunOutput: Hello Interfaces are not the only way to achieve multiple inheritance in Java. We can also achieve multiple inheritance by using the Composition approach. Using CompositionThis approach uses the target class to create objects of the appropriate classes, which are then assigned method calls to achieve code reuse without the complexities of the inheritance. It can promote better encapsulation and flexibility of the code. Let's discuss this approach with an illustration. Multiple Inheritance Java Program Using Composition ExampleCompile and RunOutput: An animal is eating. The vehicle is being driven. Explanation Two classes, Animal and Vehicle, in the example above, stand for two distinct behaviors. 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 behaviors 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. Diamond ProblemIn Java, the diamond problem is related to multiple inheritance. Sometimes, it is also known as the deadly diamond problem or the deadly diamond of death. One such challenge is the Diamond Problem, which arises in the context of multiple inheritance. The Diamond Problem emerges when a class inherits from two or more classes, which in turn share a common superclass. This scenario creates ambiguity in the inheritance hierarchy, as illustrated by the diamond-shaped dependency graph, hence the name Diamond Problem. It is an ambiguity that can arise as a consequence of allowing multiple inheritance. It is a serious problem for other OPPs languages. It is sometimes referred to as the deadly diamond of death. Resolution StrategiesTo resolve the Diamond Problem in scenarios involving default methods, Java provides several strategies:
The Solution of Diamond ProblemThe solution to the diamond problem is default methods and interfaces. We can achieve multiple inheritance by using these two things. The default method is similar to the abstract method. The only difference is that it is defined inside the interfaces with the default implementation. We need not to override these methods because they are already implementing these interfaces. The advantage of interfaces is that they can have the same default methods with the same name and signature in two different interfaces. It allows us to implement these two interfaces from a class. We must override the default methods explicitly with its interface name. Syntax: Diamon Problem Java ProgramExampleOutput: the display() method of DemoInterface1 invoked the display() method of DemoInterface2 invoked In the above example, we see that both interfaces have the same method name and signature. We have invoked the method by an interface name that does not create any ambiguity between methods. Next TopicAdvantages of JavaBeans |
Application quality is critical to the development of software systems, especially large-scale ones. High quality software would reduce the cost of software maintenance, and it enhances the potential software reuse. In order to measure the software quality more quantitatively and objectively, software metrics (MOOD) give impression to...
5 min read
Instance Variable Hiding only occurs when the subclass defines a variable with the same name as a variable in its superclass. When the subclass instance accesses the variable, the subclass variable will be used. The superclass variable can still be accessed using the super keyword. Program 1:...
4 min read
Permutation Java To determine the lexicographically permutation of a sequence of elements, apply the permutation method. It refers to rearranging the items of an array into the following larger permutation in lexicographical order. Rearranging items to produce the lexicographically permutation is the foundation...
6 min read
abstract Keyword in Java The Java abstract keyword is a non-access modifier used with classes and methods to achieve abstraction. Purpose of abstract Keyword The abstract keyword facilitates abstraction by allowing us to define a blueprint or a contract for classes without providing complete implementation details. It promotes...
5 min read
The Adapter design pattern in Java is a way to make two objects with different interfaces work together. Sometimes, we have objects that we want to use together, but their interfaces are not compatible. In such cases, we can use the Adapter pattern. The adapter pattern acts...
4 min read
Continuous Integration (CI) and Continuous Delivery (CD) have become integral components of modern software development practices. These methodologies aim to enhance collaboration, improve code quality, and accelerate the delivery of software. Java, being a widely-used programming language for building robust and scalable applications, has a plethora...
2 min read
Java, being a versatile and powerful programming language, offers a wide range of operators that go beyond the common arithmetic and logical operators. These lesser-known, yet incredibly useful, miscellaneous operators in Java can enhance your coding efficiency and open doors to new possibilities. In this article,...
5 min read
The offset of the buffer's first element within its underlying array can be obtained by calling the arrayOffset() method of the java.nio.DoubleBuffer class. In other words, buffer position p corresponds to array index p + array Offset() if this buffer is supported by an array. We...
3 min read
In this section, we will learn about the left view of a binary tree in Java with different approaches to achieve it. In the left view of a binary tree, we print only those nodes of the binary tree that are visible when the binary tree...
4 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India