sheidaei
sheidaei

Reputation: 10342

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

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.

Upvotes: 4

Views: 19471

Answers (6)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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.

Upvotes: 4

LaGrandMere
LaGrandMere

Reputation: 10379

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.

Upvotes: 6

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15219

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.

Upvotes: 2

Dan
Dan

Reputation: 1018

Quick answer is no. Multiple interfaces are not multiple inheritance. Interfaces and classes are distinct concepts.

A class is a definition of the object, where as an interface is a contract the object is obliged to follow.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

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
}

Upvotes: 5

Lanaru
Lanaru

Reputation: 9721

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

Upvotes: 3

Related Questions