Interface Inheritance in Java

If you are new here, then you should read the interface first.
Here is the table content of the article will we will cover this topic.
How the interface uses inheritance?
Multiple inheritances in Java by interface inheritance?

How the interface uses inheritance Java?

As you know inheritance is an important concept/feature of Object-Oriented Programming. Inheritance is a  mechanism by which one class can acquire/inherit the features(fields and methods) of another class. Similarly, an interface can inherit another interface also. An interface can extend any number of interfaces.

The interface CollegeInfo extends the interface PersonalInfo. That means, that the CollegeInfo inherits all fields and methods from PersonalInfo. If a class implements CollegeInfo, that class has to implement all methods defined in both CollegeInfo and PersonalInfo. Another, You can define methods in a CollegeInfo with the same signature as methods defined in a PersonalInfo.

How to extends multiple interfaces

A class can’t extend more than one class, but an interface can extend any number of interfaces. We will see how to extends multiple interfaces in Java.
You specify that by listing the names of all interfaces to inherit from, separated by a comma.

A class can extend to another class and can also implement one or more interfaces. Finally, We will see how we can do this. Here we have created a class FirstClass and two more interfaces that are FirstInterface and Secondnterface. We have created one more class(Derived class) separately that extends the FirstClass and also implements both the interfaces. The Derived class has to implement all methods defined in both FirstInterface and SecondInterface.

interface inheritance in java
interface FirstInterface 
{ 
    void showFirstInterface(); 
} 
  
interface SecondInterface 
{ 
    void showSecondInterface();
} 

class FirstClass
{
	void showFirstClass()
	{
		System.out.println("This is the method of FirstClass");
	}
}
  
// class implements both interfaces 
// and provides implementation to the method. 
class DerivedClass extends FirstClass implements FirstInterface, SecondInterface 
{
	@Override
	public void showSecondInterface() 
	{
		System.out.println("It is implementation of Second interface");
	}

	@Override
	public void showFirstInterface() 
	{
		System.out.println("It is implementation of First interface");
	} 
}
  
public class MainClass
{ 
    public static void main (String[] args) 
    { 
        DerivedClass derived = new DerivedClass(); 
  
        // calling the method implemented 
        // within the class. 
        derived.showFirstInterface();
        derived.showSecondInterface(); 
        derived.showFirstClass();	
    } 
}

Output:
It is the implementation of the First interface
It is the implementation of the Second interface
This is the method of FirstClass

An interface can extend to another interface. If an interface extends any other interface, all the abstract methods of the base interface are copied to the derived interface.

Multiple inheritances using interface in java

In JAVA 8 you can define default methods in an interface. By use of default methods, you can provide a default implementation of methods. Also, a class can implement any number of interfaces. If interfaces contain the same default methods with the same signature, the implementing class should explicitly specify which default method is to be overridden.

interface FirstInterface 
{ 
    // default method 
    default void showData() 
    { 
        System.out.println("Default method of FirstInterface"); 
    } 
} 
  
interface SecondInterface 
{ 
    // Default method 
    default void showData() 
    { 
        System.out.println("Default method of SecondInterface"); 
    } 
} 
  
// Implementation class code 
public class MainClass implements FirstInterface, SecondInterface 
{ 
    @Override
     public void showData() {
		FirstInterface.super.showData();
		SecondInterface.super.showData();
	}
	public static void main(String args[]) 
    { 
		ExampleOfSleepMethod d = new ExampleOfSleepMethod(); 
        d.showData(); 
    } 
}

Output: Default method of FirstInterface
Default method of SecondInterface

As you can see in the above example, we are implementing two interfaces (FirstInterface and SecondInterface) in class MainClass. Hence, The MainClass overrides the show() method. To access the method from different interfaces we are using super keyword. So, You can resolve the ambiguity of multiple interfaces by the use of a super keyword.

Leave a Comment