Java does not support multiple inheritance of classes, but you can use an inner class to inherit the implementation of more classes.
In the following example, the target class needs to inherit two abstract classes (A and B). The target class C itself inherits Class A and inherits another class B using an anonymous inner class.
Multiimplementation.java
abstract class A {
abstract void methodA();
}
abstract class B {
abstract void methodB();
}
class C extends A {
@Override
void methodA() {
System.out.println("extends A's method");
}
B getB() {
return new B() {
@Override
void methodB() {
System.out.println("extends B's method");
}
};
}
}
public class MultiImplementation {
public void doA(A a) {
a.methodA();
}
public void doB(B b) {
b.methodB();
}
public static void main(String[] args) {
MultiImplementation mi = new MultiImplementation();
C c = new C();
mi.doA(c);
mi.doB(c.getB());
}
}
Results:
extends A's method
extends B's method