Inheriting one interface from another interface in Java
An interface can extend another interface in Java. This is different from the typical use of interface where they need to be implemented by using the implements keyword. As with extends keyword and classes, the extends keyword is used for inheritance of interfaces.
An interface can inherit the constants and abstract methods from another interface. In this Java tutorial, we shall see sample code for inheritance among interfaces and also see how we can use this inheritance concept in real world Java applications:
Super.java
package com.example;
public interface Super {
public int method1();
public int method2();
}
Sub.java
package com.example;
public interface Sub extends Super{
public int method3();
}
Main.java
package com.example;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) {
Class classObject = Sub.class;
Method[] methods = classObject.getMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
}
}
}
Output
When we run the main method in class Main we see the following output:
method3
method1
method2
Explanation
The above output clearly shows that the interface “Sub” has inherited two methods viz. “method1″ and “method2″ from the super interface named as “Super”.
The main method above is using reflection to print the name of methods present in the “Sub” interface. Though the above code example is for inheritance of methods, we can add some constants in the “Super” interface which will then be inherited by the “Sub” interface.
Practical Use of Interface inheritance in Java
When the super type will remain abstract for ever then it should be made interface. But in addition to that, if the sub type will always remain abstract then we should also make the sub type as interface and extend from the super type. A classical example is given in “Thinking in Java” book which goes like:
interface Monster {
void menace();
}
interface DangerousMonster extends Monster {
void destroy();
}
interface Lethal {
void kill();
}
class DragonZilla implements DangerousMonster {
public void menace() {
}
public void destroy() {
}
}
interface Vampire extends DangerousMonster, Lethal {
void drinkBlood();
}
class VeryBadVampire implements Vampire {
public void menace() {
}
public void destroy() {
}
public void kill() {
}
public void drinkBlood() {
}
}
public class HorrorShow {
static void u(Monster b) {
b.menace();
}
static void v(DangerousMonster d) {
d.menace();
d.destroy();
}
static void w(Lethal l) {
l.kill();
}
public static void main(String[] args) {
DangerousMonster barney = new DragonZilla();
u(barney);
v(barney);
Vampire vlad = new VeryBadVampire();
u(vlad);
v(vlad);
w(vlad);
}
}
Explanation
In the above code we can see that “Monster” can only create “menance” but a “DangerousMonster” can also “destroy” few things. Thus a “DangerousMonster” is a “Monster” that can destroy things and hence it should have all the properties of a Monster. A “DragonZilla” is an example of “DangerousMonster” which means that while DangerousMonster is abstract in nature, “DragonZilla” is a concrete implementation of the same.





