Inheriting from multiple classes in Java (and possibly not using interface)

12,782

Solution 1

Multiple inheritance is not possible.

Anyway, you could try to simulate such inheritance with composition.

JB Nizet gave an answer to your question in the link provided.

Solution 2

From Java SE 8, interfaces may have limited implementation. This gives a form of multiple inheritance of implementation.

As an alternative to multiple inheritance, inner classes can perform a similar job.

class Derived extends BaseOne {
    ... fields ...
    private class InnerTwo extends BaseTwo {
        ... code with access to Derived's fields ...
    }
}

If methods overridden from BaseOne need to call methods of BaseTwo they will need an explicit reference to the inner class, but not the other way around and the Derived fields are shared.

Solution 3

No. The Java approach to multiple inheritance is to use interfaces.

Solution 4

A class in Java can inherit from exactly one class (if you do not specify the base class, it's java.lang.Object). The only way to inherit from three classes is if they inherit from each other in a chain:

class A {}
class B extends A {}
class C extends B {}
class D extends C {}

Now your D class inherits from A, B, and C, but I doubt that this is what you were looking for.

The idiomatic solution in Java is to implement multiple interfaces instead of inheriting multiple classes. If you must "inherit" implementation, use composition instead of inheritance.

interface IA {}
interface IB {}
interface IC {}
class A implements IA {}
class B implements IB {}
class C implements IC {}
class D implements IA, IB, IC {
    A a = ...;
    B b = ...;
    C c = ...;
    // Implementations of methods from IA, IB, and IC
    // need to forward the calls to a, b, and c
}

Solution 5

Java doesn't support multiple inheritance in the sense of class D extends A, B, C. As mentioned by Lanaru, the preferred way to do this is to use interfaces.

Otherwise, you could do something like this:

public class A {}
public class B extends A {}
public class C extends B {}
public class D extends C {}

In effect, D would be extending A, B indirectly though C.

Share:
12,782
sheidaei
Author by

sheidaei

shahin.sheidaei.com

Updated on June 05, 2022

Comments

  • sheidaei
    sheidaei almost 2 years

    So, let's say we have classes A, B and C and I want to inherit from all those classes and have another class called D, it can be done using implements and interfaces in Java. But let's say we don't want to use this simple solution, would you be able to inherit class D from classes A, B and C in any other way in Java?

    (This question might be related to design patterns, it has been brought up after challenging my colleague at lunch discussing design patterns) I don't think there is any other way to have multiple inheritance in Java other than using multiple interfaces.

  • sheidaei
    sheidaei over 11 years
    interesting approach, I haven't though of this approach before