Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The One-Parent Rule: Why Java Doesn't Support Multiple Inheritance
3.
Java's Alternative: Interfaces
3.1.
How to Use Interfaces
3.2.
Implementing Multiple Interfaces
4.
Composition: The Unsung Hero
4.1.
What is Composition?
4.2.
Using Composition
5.
Frequently Asked Questions
5.1.
Why doesn't Java support multiple inheritance?
5.2.
Can a Java class implement multiple interfaces?
5.3.
Is composition better than inheritance?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java Extend Multiple Classes

Author Lekhika
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

If you've been dabbling in Java for a while, you've probably come across the concept of inheritance, denoted by the extends keyword. It allows a class to adopt the attributes and behaviors (fields and methods) of another class. However, you might have found yourself stuck at a crossroad, pondering, "Can a Java class extend multiple classes?"

Java Extend Multiple Classes

This article aims to dissect this question in detail, explore the reasons behind Java's design choices, and introduce alternative approaches to achieve similar functionality.

Check this out, addition of two numbers in java

The One-Parent Rule: Why Java Doesn't Support Multiple Inheritance

In Java, a class can inherit attributes and methods from only one parent class. This design decision was intentional to avoid the "Diamond Problem," a complication that arises in languages that support multiple inheritance.

The Diamond Problem Explained

Imagine a class A with a method doSomething(). Classes B and C both extend A and override doSomething(). Now, let's say a class D wants to extend both B and C. The question becomes: Which doSomething() method should D inherit? From B or C?

This ambiguity is known as the Diamond Problem, and it complicates both language design and program logic.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Java's Alternative: Interfaces

Although Java doesn't support extending multiple classes, it offers a workaround through interfaces. An interface is like a contract that specifies what methods a class should implement.

How to Use Interfaces

Declare an interface using the interface keyword. Unlike classes, interfaces can't contain implementation code.

public interface Walks {
    void walk();
}

A class can implement an interface using the implements keyword.

public class Human implements Walks {
    public void walk() {
        System.out.println("Walking...");
    }
}

Implementing Multiple Interfaces

The beauty of interfaces is that a single class can implement multiple interfaces.

public interface Eats {
    void eat();
}


public class Human implements Walks, Eats {
    public void walk() {
        System.out.println("Walking...");
    }


    public void eat() {
        System.out.println("Eating...");
    }
}

Composition: The Unsung Hero

Another powerful technique to achieve the effect of multiple inheritance is composition.

What is Composition?

Composition involves creating instance variables that refer to other objects. In other words, instead of inheriting features from a parent class, you include instances of the parent class in your new class.

Using Composition

Here's a simple example:

public class Engine {
    void start() {
        System.out.println("Engine started.");
    }
}


public class Car {
    // Composition: Car has an Engine
    Engine carEngine = new Engine();


    void startCar() {
        carEngine.start();
    }
}


Also see, Java Ioexception

Frequently Asked Questions

Why doesn't Java support multiple inheritance?

To avoid the Diamond Problem, Java was designed to disallow multiple inheritance, thus making it easier to manage and understand the code.

Can a Java class implement multiple interfaces?

Yes, a Java class can implement multiple interfaces, offering a way to circumvent the lack of multiple inheritance.

Is composition better than inheritance?

Neither is universally better; it depends on your specific needs. Composition offers more flexibility but may require more boilerplate code.

Conclusion

While Java doesn't permit extending multiple classes, its design offers alternative ways to mimic the benefits of multiple inheritance. Through the clever use of interfaces and composition, Java provides flexible and clean mechanisms to share behaviors across classes. Understanding these concepts not only clarifies why Java is designed the way it is but also equips you with the tools to build more modular and maintainable applications.

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, CodeStudio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Live masterclass