Interface inheritance in java
The concept of inheritance is not limited to classes only, but applies to interfaces as well. But it is slightly different in comparison with classes. Below are features of inheritance of interfaces
- One interface can extend one or more interfaces.
- All the methods of the parent interface are inherited by the child interface.
- All the constants are also inherited by the child interface.
- Default/static methods are also inherited by the child interface.
Interface inheritance syntax
interface ABC extends XYZ {
}
or
interface C extends A, B {
}
Example
A.java
package com.sks.inheritance;
public interface A {
void display();
}
B.java
package com.sks.inheritance;
public interface B {
void print();
}
C.java
package com.sks.inheritance;
public interface C extends A, B {
//This inherits display() from interface A and print() from interface B
}
Main.java
package com.sks.inheritance;
public class Main {
public static void main(String[] args) {
C c = new CImpl();
c.display();
c.print();
}
}
Output :
From inside display
From inside print