«Back to Home

Core Java

Topics

Interface In Java

Interface

An interface in Java is similar to a class and we can say that it is a blueprint of a class.
  • Interface is mainly used to achieve full abstraction in Java.

  • Interface has static constants and abstract method body only and not method body.

  • Interface supports multiple inheritance in Java.

  • An interface shows IS-A relationship also.

  • Interface can’t be instantiated, as in case of an abstract class.
Main advantages of interface in Java are,
  1. To achieve full abstraction.

  2. To achieve multiple inheritance.

  3. To achieve loose coupling.
In Java, the compiler by default adds public and abstract keywords before the interface method and public, static and final keyword before the variables. It means that all the variables of an interface are public, static, final and methods are public and abstract.

67
 
Relationship between class and interface
  • A class extends another class.

    30

  • An interface extends another interface.

    28

  • A class implements an interface.

    29
Now, let’s see an example of an interface, given below.
 
Code
  1. interface Car {  
  2.     void run();  
  3. }  
  4. public class Audi implements Car {  
  5.     public void run() {  
  6.         System.out.println("Audi is a fast car");  
  7.     }  
  8.     public static void main(String args[]) {  
  9.         Audi obj = new Audi();  
  10.         obj.run();  
  11.     }  
  12. }  
31
 
Output

32

Multiple inheritance through interface

Multiple inheritance means when a class implements multiple interfaces or an interface extends multiple interfaces.

39
 
Let’s see an example of multiple inheritance in Java through an interface, given below.
 
Code
  1. interface X {  
  2.     void msg();  
  3. }  
  4. interface Y {  
  5.     void call();  
  6. }  
  7. public class Z implements X, Y {  
  8.     public void msg() {  
  9.         System.out.println("Hello, X");  
  10.     }  
  11.     public void call() {  
  12.         System.out.println("Hello, Y");  
  13.     }  
  14.     public static void main(String args[]) {  
  15.         Z obj = new Z();  
  16.         obj.msg();  
  17.         obj.call();  
  18.     }  
  19. }  
33

Output

34

In Java, Multiple inheritance is not supported through class. It is possible through an interface because interface methods implementation provided by the other class. Hence, there is no ambiguity.
 
Let’s see an example, given below.
 
Code
  1. interface X {  
  2.     void msg();  
  3. }  
  4. interface Y {  
  5.     void msg();  
  6. }  
  7. public class Z implements X, Y {  
  8.     public void msg() {  
  9.         System.out.println("Hello, I am message");  
  10.     }  
  11.     public static void main(String args[]) {  
  12.         Z obj = new Z();  
  13.         obj.msg();  
  14.     }  
  15. }  
35

Output

36

In the example, shown above, X and Y interface have same methods but its implementation is provided by class Z due to which there is no ambiguity.
 
Interface inheritance in Java

In Java, interface inheritance means one interface extends another interface.
Let’s see an example, given below.
Code
  1. interface Car {  
  2.     void run();  
  3. }  
  4. interface Bike extends Car {  
  5.     void stop();  
  6. }  
  7. public class ExampleTest implements Bike {  
  8.     public void run() {  
  9.         System.out.println("Car running...");  
  10.     }  
  11.     public void stop() {  
  12.         System.out.println("Bike stop...");  
  13.     }  
  14.     public static void main(String args[]) {  
  15.         ExampleTest obj = new ExampleTest();  
  16.         obj.run();  
  17.         obj.stop();  
  18.     }  
  19. }  
37
Output

38

Marker interface

In Java, an interface has no members or no body due to which, it is called as a marker interface or tagged interface. For example- Serializable, Cloneable, Remote etc. Marker interface is mainly used to provide some essential information to JVM and JVM is used it to perform some useful operations.
 
Nested interface

When an interface is declared in another interface, it is called as a nested interface in Java.
 
Summary

Thus, we learnt that an interface is mainly used to achieve full abstraction in Java and also learnt multiple inheritance through an interface.