6
Dec
An interface in Java is a blueprint of a class. It is similar to class. It is a collection of abstract methods. Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
Here is an example of multiple inheritance with interface:
interface Parent1{
default void show(){
System.out.println("This is from parent 1");
}
}
interface Parent2{
default void show(){
System.out.println("This is from parent 2");
};
}
class Child1 implements Parent1,Parent2{
public void show(){
Parent1.super.show();
Parent2.super.show();
}
}
class Main {
public static void main(String[] args) {
Child1 child1=new Child1();
child1.show();
}
}
Output:
This is from parent 1
This is from parent 2
Leave a Reply