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.

Inheriting one interface from another interface in Java admin Core 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...
An interface can extend another interface in Java. This is different from the typical use of <a href="http://www.javaexperience.com/android-create-non-blocking-user-interface/" title="Create Non-Blocking User Interface in Android">interface</a> 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 <a href="http://www.javaexperience.com/multiple-inheritance-in-java/" title="Multiple inheritance in Java">sample code for inheritance</a> among interfaces and also see how we can use this inheritance concept in real world Java applications: <strong>Super.java</strong> 1 <strong>Sub.java</strong> 1 <strong>Main.java</strong> 1 <strong>Output</strong> When we run the main method in class Main we see the following output: <blockquote> method3 method1 method2 </blockquote> <strong>Explanation</strong> 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 <a href="http://www.javaexperience.com/main-method-in-java/" title="How main Method works in Java">main method</a> 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. <h2>Practical Use of Interface inheritance in Java</h2> When <a href="http://www.javaexperience.com/this-and-super-in-java/" title="this and super keywords">the super type</a> 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: 1 <strong>Explanation</strong> 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.