For working professionals
For fresh graduates
More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
Foreign Nationals
The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not .
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
Inheritance is a cornerstone of Java, allowing a new class to reuse the methods and fields of an existing class. It’s a powerful tool for building logical relationships. A common question that arises is whether a class can inherit from more than one parent. While the concept of multiple inheritance in java is powerful, it introduces significant complexity.
This complexity is the primary reason why multiple inheritance is not supported in java for classes. To prevent ambiguity and keep the language robust, Java offers a more elegant solution: implementing multiple interfaces.
This tutorial will break down this important design choice, exploring how you can achieve code reuse and flexibility in your applications. Let’s start by understanding Multiple Inheritance.
Advance your Java programming expertise with our Software Engineering courses and elevate your learning to the next level.
Multiple inheritance in Java refers to the power of a class to inherit characteristics from more than one superclass at the same time. In the case of multiple inheritances, we can say that classes derive properties from multiple parent classes, thus forming class hierarchies.
Inheritance is a cornerstone concept In Java programming, offering many advantages and allowing efficient, modular, and reusable code. Here we will delve into the compelling reasons why Java developers widely embrace inheritance, exploring the key benefits it brings to the table.
Polymorphism refers to the ability of objects to take on multiple forms or behaviors.
You can create a hierarchy of related classes, each inheriting from a common superclass.
Inheritance also grants developers the power to override methods defined in the superclass within the subclass.
Accelerate your tech career by mastering future-ready skills in Cloud, DevOps, AI, and Full Stack Development. Gain hands-on experience, learn from industry leaders, and develop the expertise that top employers demand.
The mechanism known as method overriding lets you provide specialized implementations of methods in the derived classes.
By selectively overriding methods, you can customize and extend the behavior of the inherited functions tailoring it to suit the specific requirements of the subclass.
Also Read: Difference Between Overloading and Overriding in Java: Understanding the Key Concepts in 2025
This comprehensive guide will shed light on the essential and significant terms used in inheritance. By learning these terms, you'll gain clarity on the intricacies of inheritance and be able to wield its power effectively in your Java code. Let's explore the key terms associated with inheritance in Java.
The superclass or base class is the existing class from which other classes inherit properties and behaviors. It serves as the foundation for the derived classes and defines the common characteristics inherited by its subclasses
A subclass or derived class is a new class created by inheriting properties and behaviors from a superclass. It specializes in the working of the superclass by adding new features or modifying existing ones.
The "extends" keyword is used in Java to establish an inheritance relationship between classes. It allows a subclass to inherit from a superclass. By specifying the superclass after the "extends" keyword, the subclass gains access to all the public and protected members of the superclass
The "super" keyword in Java is used within a subclass to refer to its superclass. It can be used to access superclass members, invoke superclass constructors, and differentiate between superclass and subclass members with the same name.
Method overriding is when subclasses implement methods already defined in superclasses. By overriding a method, the subclass can customize or extend the behavior inherited from the superclass.
The "super()" constructor invocation is used in a subclass constructor to call the constructor of its superclass. It ensures that the superclass constructor is executed before the subclass constructor, allowing the initialization of inherited members.
The "final" keyword is used in Java to indicate that a class, method, or variable cannot be extended, overridden, or modified, respectively. When applied to a class, it prevents inheritance, making it the final class in the hierarchy.
Also Read: This and Super Keyword in Java
Here is an example of inheritance in Java:
// Parent class
class Vehicle {
protected String brand;
protected int year;
public Vehicle(String brand, int year) {
this.brand = brand;
this.year = year;
}
public void honk() {
System.out.println("Honk honk!");
}
}
// Child class inheriting from Vehicle
class Car extends Vehicle {
private int numberOfDoors;
public Car(String brand, int year, int numberOfDoors) {
super(brand, year);
this.numberOfDoors = numberOfDoors;
}
public void drive() {
System.out.println("Driving the car");
}
}
// Child class inheriting from Vehicle
class Motorcycle extends Vehicle {
private boolean hasSidecar;
public Motorcycle(String brand, int year, boolean hasSidecar) {
super(brand, year);
this.hasSidecar = hasSidecar;
}
public void wheelie() {
System.out.println("Doing a wheelie!");
}
}
// Main class to test the inheritance
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", 2020, 4);
car.honk();
car.drive();
Motorcycle motorcycle = new Motorcycle("Harley-Davidson", 2021, false);
motorcycle.honk();
motorcycle.wheelie();
}
}
In this example, we have a parent class called Vehicle with two instance variables brand and year, and a method honk(). The child classes Car and Motorcycle inherit from the Vehicle class.
The Car class adds an additional instance variable numberOfDoors and a method drive(). The Motorcycle class adds an additional instance variable hasSidecar and a method wheelie().
In the Main class, we create objects of the Car and Motorcycle classes and call their respective methods to demonstrate inheritance.
Apart from multiple inheritance, Java has three kinds of inheritance. Let us explore them.
Here is an example of single inheritance in Java:
// Parent class
class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println(name + " is barking.");
}
}
// Main class to test the single inheritance
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.eat();
dog.bark();
}
}
In this example, we have a parent class called Animal with an instance variable name and a method eat(). The child class Dog inherits from the Animal class.
The Dog class adds a method bark(). It can access the name variable and the eat() method from the parent class.
In the Main class, we create an object of the Dog class and call its methods to demonstrate single inheritance.
Here is an example of multilevel inheritance in Java:
// Grandparent class
class Animal {
protected String species;
public Animal(String species) {
this.species = species;
}
public void eat() {
System.out.println(species + " is eating.");
}
}
// Parent class inheriting from Animal
class Dog extends Animal {
public Dog() {
super("Dog");
}
public void bark() {
System.out.println("Dog is barking.");
}
}
// Child class inheriting from Dog
class Labrador extends Dog {
public Labrador() {
System.out.println("Labrador is a type of Dog.");
}
public void playFetch() {
System.out.println("Labrador is playing fetch.");
}
}
// Main class to test the multilevel inheritance
public class Main {
public static void main(String[] args) {
Labrador labrador = new Labrador();
labrador.eat();
labrador.bark();
labrador.playFetch();
}
}
In this example, we have a grandparent class called Animal with an instance variable species and a method eat(). The parent class Dog inherits from the Animal class and adds a method bark(). The child class Labrador inherits from the Dog class and adds a method playFetch().
In the Main class, we create an object of the Labrador class and call its methods to demonstrate multilevel inheritance.
Here is an example of hierarchical inheritance in Java:
// Parent class
class Animal {
protected String species;
public Animal(String species) {
this.species = species;
}
public void eat() {
System.out.println(species + " is eating.");
}
}
// Child class 1 inheriting from Animal
class Dog extends Animal {
public Dog() {
super("Dog");
}
public void bark() {
System.out.println("Dog is barking.");
}
}
// Child class 2 inheriting from Animal
class Cat extends Animal {
public Cat() {
super("Cat");
}
public void meow() {
System.out.println("Cat is meowing.");
}
}
// Main class to test the hierarchical inheritance
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
In this example, we have a parent class called Animal with an instance variable species and a method eat(). The child class Dog inherits from the Animal class and adds a method bark(). The child class Cat also inherits from the Animal class and adds a method meow().
In the Main class, we create objects of the Dog and Cat classes and call their respective methods to demonstrate hierarchical inheritance.
Also Read: What are the Types of Inheritance in Java? Examples and Tips to Master Inheritance
Java does not directly support multiple inheritance, where classes can inherit from multiple parent classes. However, you can achieve similar functionality using interfaces in Java.
Here's an example code that demonstrates a workaround for achieving multiple inheritance-like behavior using interfaces:
// First parent interface
interface Animal {
void eat();
}
// Second parent interface
interface Mammal {
void run();
}
// Child class implementing both interfaces
class Dog implements Animal, Mammal {
@Override
public void eat() {
System.out.println("Dog is eating.");
}
@Override
public void run() {
System.out.println("Dog is running.");
}
}
// Main class to test multiple inheritance-like behavior
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.run();
}
}
In this example, we have two parent interfaces: Animal and Mammal. The Animal interface declares the eat() method, and the Mammal interface declares the run() method.
The Dog class implements the Animal and Mammal interfaces, effectively inheriting from both. It provides implementations for the eat() and run() methods.
In the Main class, we create an object of the Dog class and call its methods to demonstrate multiple inheritance-like behavior.
Also Read: Understanding the Differences Between Inheritance and Polymorphism in Java
Java intentionally omits multiple inheritance. Let's examine the rationale behind Java's decision to exclude multiple inheritance.
Java avoids the diamond problem and maintains code stability by excluding multiple inheritance.
This design choice enhances code clarity, modularity, and extensibility, allowing developers to compose classes using interfaces and achieve similar benefits to multiple inheritance without the associated complexities.
Understanding why multiple inheritance is not supported in java using classes is key to appreciating Java's design philosophy, which prioritizes simplicity and robustness over complexity. The language deliberately avoids the "Diamond Problem" to prevent ambiguity in your code.
However, as this tutorial has shown, you can still achieve the benefits of code reuse and flexibility. By mastering alternatives like interfaces and composition, you can effectively implement the principles of multiple inheritance in java in a clean, maintainable, and error-free way.
The primary reason why multiple inheritance is not supported in java for classes is to avoid the Diamond Problem. This is a classic ambiguity that arises when a class inherits from two parent classes that both share a common grandparent. If a method is defined in the grandparent and overridden by both parents, the final child class is left with two different versions of the same method, and the compiler has no logical way to decide which one to use. Java's creators chose to avoid this complexity to keep the language simpler and more robust.
The Diamond Problem is the core reason why multiple inheritance is not supported in java. Imagine a base class A with a method doSomething(). Two classes, B and C, both inherit from A and override doSomething(). Now, if another class D tries to inherit from both B and C, it creates a diamond-shaped inheritance diagram. The problem is: which version of doSomething() should class D inherit? The one from B or the one from C? This ambiguity is what Java avoids by disallowing this type of inheritance for classes.
Java cleverly solves the Diamond Problem by allowing a class to implement multiple interfaces. Before Java 8, interfaces could only have abstract methods (methods without a body). This meant that even if a class implemented two interfaces that had a method with the same signature, there was no conflict because the implementing class was responsible for providing the single, definitive implementation for that method. This is a key part of the design of multiple inheritance in java using interfaces.
No, a class in Java can only extend one other class. This is the principle of single inheritance. However, a class can implement any number of interfaces. This "extend one, implement many" model is Java's way of getting the benefits of code reuse from a single parent while also gaining the flexibility of multiple behaviors from interfaces.
The extends keyword is used for class-to-class inheritance, where a subclass inherits the fields and methods of a single superclass. The implements keyword is used for class-to-interface implementation, where a class promises to provide the implementation for all the abstract methods defined in one or more interfaces. extends creates an "is-a" relationship, while implements creates a "can-do" relationship.
The primary and most common alternative is to use interfaces. A class can implement multiple interfaces, allowing it to inherit the "type" and required behaviors of several different sources. Another powerful alternative is composition, where instead of inheriting from a class, you create an instance of that class as a private field and delegate method calls to it. This "composition over inheritance" principle is a cornerstone of good object-oriented design.
While you cannot achieve true multiple inheritance in java (inheriting implementation from multiple classes), you can achieve what is known as "multiple inheritance of type" by implementing multiple interfaces. This means a class can take on the form of multiple types, which is essential for polymorphism. Since Java 8, with the introduction of default methods, interfaces can also provide implementation, allowing you to achieve a form of implementation inheritance as well.
Default methods, introduced in Java 8, allow interfaces to have methods with a default implementation. This brings Java closer to a form of multiple inheritance in java, as a class can now inherit implemented methods from multiple interfaces. However, Java has rules to prevent the Diamond Problem from reappearing. If a class implements two interfaces that have a default method with the same signature, the class is forced to override that method and provide its own implementation, thus resolving the ambiguity.
Composition is an object-oriented design principle where a class is "composed" of other objects, meaning it contains instances of other classes as its fields. Instead of inheriting a behavior, the class delegates the task to the object it contains. For example, instead of a Car class inheriting from both a Vehicle class and an Engine class, the Car class would simply contain an Engine object as a field. This is a more flexible and less coupled alternative to multiple inheritance in java.
This is a widely accepted design principle that suggests you should favor composition over class inheritance. Inheritance creates a very tight coupling between a parent and child class. Composition is more flexible, as the composed objects can be changed at runtime, and it leads to a more modular and easier-to-maintain design. It is the primary way to achieve the benefits of code reuse without the rigidity of multiple inheritance in java.
The "limitations" are actually intentional design choices to keep Java simple and robust. You can overcome them by using Java's intended patterns. Use interfaces to define common behaviors that can be shared across different class hierarchies. Use composition and delegation to reuse implementation from multiple sources by creating instances of those classes within your class. These patterns provide all the flexibility you need without the complexities associated with the classic model of multiple inheritance in java.
Yes, both C++ and Python support multiple inheritance with classes. They each have their own mechanisms for dealing with the potential complexities like the Diamond Problem. C++, for example, uses a technique called "virtual inheritance" to resolve ambiguity. Python uses a "Method Resolution Order" (MRO) algorithm to determine which parent's method to call in a predictable way.
An interface in Java is a completely abstract type that is used to group related methods with empty bodies. A class can then implement an interface, which means it promises to provide the code for all the methods in that interface. An interface defines what a class can do, but not how it does it. This is the foundation for how Java achieves a safe and effective form of multiple inheritance in java.
Yes, an interface can extend one or more other interfaces. This allows you to build a hierarchy of interfaces, where a child interface inherits all the abstract methods of its parent interfaces. A class that then implements this child interface must provide an implementation for all the methods from the entire inheritance chain.
In Java's own standard library, the ArrayList class implements several interfaces, including List, RandomAccess, and Serializable. This means an ArrayList object "is a" List, "can do" fast random access, and "can be" serialized. This is a perfect example of using interfaces to achieve the benefits of multiple inheritance in java by allowing a single class to take on multiple roles.
A marker interface is an empty interface with no methods or constants. Its sole purpose is to "mark" a class as having a certain capability. A classic example is the Serializable interface. A class that implements Serializable is signaling to the Java runtime that it is okay for its objects to be serialized. This is another way Java uses its interface system to add behaviors to classes.
Yes, this is a very common and powerful pattern in Java. A class can extend one parent class to inherit its core functionality and state, and then implement multiple interfaces to gain additional capabilities or behaviors. For example: public class MyClass extends MyBaseClass implements Runnable, Serializable.
A "mixin" is a class that contains methods for use by other classes, but it is not meant to be instantiated on its own. In languages with multiple inheritance, you can inherit from a mixin to add its functionality. In Java, a similar effect can be achieved since Java 8 using interfaces with default methods. You can implement an interface to "mix in" its default method implementations into your class.
The best way to learn is through a combination of structured education and hands-on practice. A comprehensive program, like the software development courses offered by upGrad, can provide a strong foundation in OOPS principles and design patterns. You should also study the classic "Gang of Four" design patterns book and try to implement patterns like composition and delegation in your own projects to understand the practical alternatives to multiple inheritance in java.
The key takeaway is that while Java does not support inheriting from multiple classes, it provides a more robust and less ambiguous alternative through interfaces. Understanding why multiple inheritance is not supported in java (to avoid the Diamond Problem) is crucial. By correctly using interfaces and the principle of composition, you can achieve all the flexibility and code reuse of multiple inheritance in java in a way that is clean, maintainable, and aligned with modern object-oriented design principles.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
FREE COURSES
Start Learning For Free
Author|900 articles published
Recommended Programs