To reduce the complexity and simplify the language, multiple inheritances is not supported in java.
Since compile-time errors are better than runtime errors, java renders compile-time error if you inherit 2 classes. So whether you have the same method or different, there will be a compile-time error now.
In Java, we can achieve multiple inheritances only through Interfaces.
Example 1:
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
public class C extends A, B{
public static void main(String[] args) {
C obj=new C();
obj.msg();
}
}
// Error: java: ‘{‘ expected
output:
Error: java: ‘{‘ expected
Example 2:
interface A{
default void msg(){System.out.println("Hello");}
}
interface B{
default void msg(){System.out.println("Welcome");}
}
public class C implements A, B{
@Override
public void msg(){
A.super.msg();
B.super.msg();
}
public static void main(String[] args) {
C obj=new C();
obj.msg();
}
}
output :
Hello
Welcome