2

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
  • 9,842
  • 20
  • 63
  • 86
  • One way may be let classA extend ClassB and ClassB extend ClassC. ClassD extends ClassA. – kosa Aug 28 '12 at 15:34
  • 1
    If classes A, B and C all have behaviors which you wish to re-use then just use them as delegates instead? – Adam Aug 28 '12 at 15:36
  • 1
    `let's say we don't want to use this simple solution` then you would ask why not? ;) – Peter Lawrey Aug 28 '12 at 15:38
  • @Adam what do you mean by using as delegate instead? – sheidaei Aug 28 '12 at 16:33
  • @sheidaei I mean have an instance of class A inside class D and have class D forward its calls to that class. – Adam Aug 28 '12 at 16:35
  • @Adam thanks I got it now, I guess that is the best alternative to inheritance and you can do it as a design pattern as well, I am not sure, but I think it is bridge design pattern – sheidaei Aug 28 '12 at 16:39
  • 1
    @Kuf thanks, I didn't know how to do it, man it goes up – sheidaei Aug 28 '12 at 16:40
  • I think you should rephrase this `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?` to `But let's say we don't want to use this simple solution, would you be able to inherit class A,B, C in class D, in any other way in Java?` – sofs1 Oct 06 '19 at 20:18

6 Answers6

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
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

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.

Community
  • 1
  • 1
LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
3

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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
2

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

Lanaru
  • 9,421
  • 7
  • 38
  • 64
1

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.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
0

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.

Dan
  • 1,030
  • 5
  • 12