Multiple inheritance and Interface in Java

Rumman Ansari   Software Engineer   2019-03-30   8837 Share
☰ Table of Contents

Table of Content:


Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.

interface in java

Program:

interface Cow{
	void cowMethod();
}
interface Dog{
void dogMethod();
}
class SaveClass implements Cow,Dog{

	public void cowMethod(){
		System.out.println("You are cow");
	}

	public void dogMethod(){
		System.out.println("But I am not Dog");
	}

public static void main(String args[]){
	SaveClass obj = new SaveClass();
	obj.cowMethod();
	obj.dogMethod();
 }
}
 

Output:

You are cow
But I am not Dog
Press any key to continue . . .
 

Multiple inheritance is not supported through class in java but it is possible by interface, because:

In case of interface there is no ambiguity as implementation is provided by the implementation class. For example:

Program:

interface Cow{
	void cowMethod();
}
interface Dog{
void cowMethod();
}
class SaveClass implements Cow,Dog{

	public void cowMethod(){
		System.out.println("You are not a Cow. But remember this is cow method");
	}
public static void main(String args[]){
	SaveClass obj = new SaveClass();
	obj.cowMethod();
 }
}
 

Output:

You are not a Cow. But remember this is cow method
Press any key to continue . . .
 

As you can see in the above example, Cow and Showable Dog have same methods but its implementation is provided by class SaveClass, so there is no ambiguity.

Interface and Inheritance

an interface can not implement another interface. It has to extend the other interface if required. See the below example where we have two interfaces Inf1 and Inf2. Inf2 extends Inf1 so If class implements the Inf2 it has to provide implementation of all the methods of interfaces Inf1 and Inf2.

Program:

  interface Inf1{
   public void method1();
}

 interface Inf2 extends Inf1 {
   public void method2();
}

 class Demo implements Inf2{
  public void method1(){
	 //Implementation of method1
	 System.out.println("Implementation of method1");
  }
  public void method2(){
    //Implementation of method2
    System.out.println("Implementation of method2");
  }

  public static void main(String args[])
  {
	 Demo d = new Demo();
	 d.method1();
	 d.method2();
	  }
}
 

Output:

 Implementation of method1
Implementation of method2
Press any key to continue . . .
 

In the program above, Demo class is implementing only one interface Inf2 however it has to provide the implementation of all the methods of interface Inf1 too, because interface Inf2 extends Inf1.

Interface and Inheritance Example 2

Remember a class implements interface but one interface extends another interface .

Program:

 interface Cow{
void cowMethod();
}

interface Dog extends Cow{
void dogMethod();
}

class InterfaceEx implements Dog{
public void cowMethod(){
	System.out.println("Hello cow");
}

public void dogMethod(){
	System.out.println("Welcome dog");
}

public static void main(String args[]){
	InterfaceEx obj = new InterfaceEx();
	obj.cowMethod();
	obj.dogMethod();
 }
}
 

Output:

Hello cow
Welcome dog
Press any key to continue . . .
 

Java 8 Default Method in Interface

Since Java 8, we can have method body in the interface. But we need to make it default method. Let's see an example:

File: InterfaceDefault.java

Program:

interface Cow{
void cowMethod1();
default void cowMethod2(){
	System.out.println("default method");
	}
}

class ImpCow implements Cow{
public void cowMethod1(){
	System.out.println("Implementation Method1");
	}
}

class InterfaceDefault{
	public static void main(String args[]){
	Cow d=new ImpCow();
	d.cowMethod1();
	d.cowMethod2();
	}
}
 

Output:

Implementation Method1
default method
Press any key to continue . . .
 

Java 8 Static Method in Interface

Since Java 8, we can have a static method in an interface. Let's see an example:

File: InterfaceStatic.java

Program:

interface Message{
     void msg();
static int cube(int x){return x*x*x;}
}
class ImpM implements Message{
public void msg(){
	System.out.println("Message msg implemented");
	}
}

class InterfaceStatic{
	public static void main(String args[]){
	Message d=new ImpM();
	d.msg();
	System.out.println(Message.cube(5));
   }
}
 

Output:

Message msg implemented
125
Press any key to continue . . .
 

Tag or Marker interface

An empty interface is known as tag or marker interface. For example Serializable, EventListener, Remote(java.rmi.Remote) are tag interfaces. These interfaces do not have any field and methods in it.

This is how a tag interface looks:

package java.util;
public interface EventListener
{}

These interfaces do not have any field and methods in it. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.

You must be thinking if they are empty why class implements them? What's the use of it?

The class implements them to claim the membership in a particular set. For example: If a class implements Serializable interface, it is claiming to be the member of Serializable classes, so if JVM (Java Virtual Machine) sees that a class is Serializable, it does some trick or special operation that helps in the serialization/de-serialization process.
Basically, Tag interfaces are meaningful to the JVM (Java virtual machine). You can also create your own tag interfaces to segregate and categorize your code. It would improve the readability of your code.

 //synytax to write Serializable interface  
 
public interface Serializable{  

}  
 

Nested Interface in Java

An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface.

Purpose of Nested Interface

Nested interface cannot be accessed directly, The main purpose of using them is to resolve the namespace by grouping related interfaces (or related interface and class) together. This way, we can only call the nested interface by using outer class or outer interface name followed by dot( . ), followed by the interface name.

Points to Remember:

  • Nested interfaces are static by default. You don’t have to mark them static explicitly as it would be redundant.
  • Nested interfaces declared inside the class can take any access modifier, however nested interface declared inside interface is public implicitly.
interface PrintIntfc{  
 void print();  
 
 interface MessagePrintable{  
   void msg();  
 }  
}  

Example 1: Nested interface declared inside another interface

Program:

interface MyInterfaceA{  
    void display();  
    interface MyInterfaceB{  
        void myMethod();  
    }  
}  
      
class NestedInterfaceDemo1 
    implements MyInterfaceA.MyInterfaceB{  
     public void myMethod(){
         System.out.println("This is Nested interface myMethod");
     }  
      
     public static void main(String args[]){  
         MyInterfaceA.MyInterfaceB obj=
                 new NestedInterfaceDemo1(); 
      obj.myMethod();  
     }  
}
 

Output:

This is Nested interface myMethod
 

Example 2: Nested interface declared inside a class

Program:

class MyClass{  
    interface MyInterfaceB{  
        void myMethod();  
    }
}  
    
class NestedInterfaceDemo2 implements MyClass.MyInterfaceB{  
     public void myMethod(){
         System.out.println("This is Nested interface myMethod");
     }  
    
     public static void main(String args[]){  
        MyClass.MyInterfaceB obj=
               new NestedInterfaceDemo2();  
        obj.myMethod();  
     }  
}
 

Output:

This is Nested interface myMethod