Extending an Interface in java
In java, an interface can extend another interface. When an interface wants to extend another interface, it uses the keyword extends. The interface that extends another interface has its own members and all the members defined in its parent interface too. The class which implements a child interface needs to provide code for the methods defined in both child and parent interfaces, otherwise, it needs to be defined as abstract class.
🔔 An interface can extend another interface.
🔔 An interface can not extend multiple interfaces.
🔔 An interface can implement neither an interface nor a class.
🔔 The class that implements child interface needs to provide code for all the methods defined in both child and parent interfaces.
Let's look at an example code to illustrate extending an interface.
interface ParentInterface{
void parentMethod();
}
interface ChildInterface extends ParentInterface{
void childMethod();
}
class ImplementingClass implements ChildInterface{
public void childMethod() {
System.out.println("Child Interface method!!");
}
public void parentMethod() {
System.out.println("Parent Interface mehtod!");
}
}
public class ExtendingAnInterface {
public static void main(String[] args) {
ImplementingClass obj = new ImplementingClass();
obj.childMethod();
obj.parentMethod();
}
}
When we run the above program, it produce the following output.