Master java skills

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

  1. One interface can extend one or more interfaces.
  2. All the methods of the parent interface are inherited by the child interface.
  3. All the constants are also inherited by the child interface.
  4. 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