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.
Can a class extend multiple classes in Java?
5.2.
How many classes can you extend in Java?
5.3.
Can you extend multiple times in Java?
6.
Conclusion
Last Updated: Dec 1, 2024
Easy

Java Extend Multiple Classes

Author Lekhika
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

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

Can a class extend multiple classes in Java?

No, a class in Java cannot extend multiple classes because Java does not support multiple inheritance to avoid ambiguity.

How many classes can you extend in Java?

In Java, a class can extend only one class at a time, as Java supports single inheritance for classes.

Can you extend multiple times in Java?

Yes, you can extend multiple times indirectly by creating a chain of inheritance where one class extends another, forming a hierarchy.

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 Code360 to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

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

Live masterclass