Javatpoint Logo
Javatpoint Logo

Java Extend Multiple Classes

Java is an object-oriented programming language that allows developers to create complex software systems. One of the key features of Java is inheritance, which allows classes to inherit properties and methods from other classes. In Java, a class can only extend one parent class at a time, but it is possible to achieve multiple inheritance-like behaviour by using interfaces. In this article, we will explore how to extend multiple classes in Java and provide example programs with output.

Extending Multiple Classes in Java

Java does not allow a class to extend multiple classes directly. The reason for this is to avoid the Diamond Problem, which occurs when a class inherits from two classes that have a common superclass. To avoid this issue, Java allows a class to implement multiple interfaces, which can achieve similar functionality.

An interface is a collection of abstract methods that define the behaviour of a class. Unlike classes, interfaces cannot be instantiated, but they can be implemented by classes. By implementing an interface, a class can inherit properties and methods from multiple interfaces. It allows a class to achieve multiple inheritance-like behaviour, without the Diamond Problem. To extend multiple classes in Java, we need to create an interface that contains the properties and methods of the parent classes. The implementing class can then implement the interface and inherit the properties and methods of the parent classes. Let's take a look at an example to better understand this concept.

Extending Multiple Classes in Java

In this example, we will create three classes: Animal, Mammal, and Reptile. We will then create an interface called Omnivore, which will extend the Animal and Mammal classes. Finally, we will create a class called Platypus, which will implement the Omnivore interface.

The Animal class will contain a method called "eat" that prints "Animal is eating." The Mammal class will extend the Animal class and contain a method called "drinkMilk" that prints "Mammal is drinking milk." The Reptile class will also extend the Animal class and contain a method called "layEggs" that prints "Reptile is laying eggs."

Here is the code for the Animal class:

Here is the code for the Mammal class:

Here is the code for the Reptile class:

Now let's create the Omnivore interface, which will extend the Animal and Mammal classes:

Finally, let's create the Platypus class, which will implement the Omnivore interface:

In this example, the Platypus class is implementing the Omnivore interface, which extends the Animal and Mammal classes. Therefore, the Platypus class is inheriting the properties and methods of both the Animal and Mammal classes.

Let's test our program by creating an instance of the Platypus class and calling its methods:

Output:

Platypus is eating.
Platypus is drinking milk.
Platypus is eating plants.

As we can see, the Platypus class is able to inherit the properties and methods of both the Animal and Mammal classes by implementing the Omnivore interface. This allows us to achieve multiple inheritance-like behaviour in Java.

When to Use Multiple Inheritance-Like Behaviour in Java

While it is possible to achieve multiple inheritance-like behaviour in Java using interfaces, it is not always necessary or desirable. In fact, many developers argue that multiple inheritance can make code more complex and difficult to maintain. Therefore, it is important to carefully consider whether multiple inheritance-like behaviour is necessary for your program.

One situation where multiple inheritance-like behavior may be useful is when you need to combine functionality from multiple sources. For example, if you have two classes that provide different types of functionality, you may want to combine them into a single class that inherits from both. In this case, implementing an interface that extends both classes can be a good solution. Another situation where multiple inheritance-like behaviour may be useful is when you are working with third-party libraries that use inheritance extensively. If you need to extend multiple third-party classes, implementing interfaces can be a good way to achieve this without creating complex inheritance hierarchies.

In Java, it is not possible to directly extend multiple classes. However, it is possible to achieve multiple inheritance-like behaviour by implementing interfaces that extend multiple classes. This allows a class to inherit properties and methods from multiple parent classes, without the Diamond Problem that can arise with direct multiple inheritance. While multiple inheritance-like behaviour can be useful in certain situations, it is important to carefully consider whether it is necessary for your program. In many cases, using interfaces and composition can be a better solution for combining functionality from multiple sources.


Next TopicJava Function





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