How to inherit from multiple base classes in Java?

java, multiple-inheritance

Solution

Multiple inheritance is not allowed in Java. Use delegates and interfaces instead

public interface AInterface {
        public void a();
}
public interface BInterface {
    public void b();
}

public class A implements AInterface {
    public void a() {}
}
public class B implements BInterface {
    public void b() {}
}

public class C implements AInterface, BInterface {
    private A a;
    private B b;

    public void a() {
        a.a();
    }
    public void b() {
        b.b();
    }
}

Since Java 8 it's possible to use Default Methods in Interfaces.

Problem

Possible Duplicates: Cheat single inheritance in Java !! Why is Multiple Inheritance not allowed in Java or C#? Multiple Inheritance in java. I know that we can use interfaces to inherit from multiple classes but is it possible to inherit the state as well? How can I inherit methods with definitions from 2 classes and have them in a third class in Java?

Original source

Related problems