Interface Keyword in Java

Introduction 

 
This article contains the exact description of interface in Java along with some easy and understandable illustrations with diagrams for practical aspects.
 

Interface

 
The interface is a keyword used in Java and is declared as an interface. Just like with classes, an interface can also be declared as public.
  
An interface is a mechanism to obtain the abstraction and multiple inheritances in Java. There can be only abstract methods in the interface without any implementation. Interfaces cannot be instantiated on their own, so must be implemented by a class or extended by another interface to be used. It also has a “is a relationship”.
  • Java compiler adds public and abstract keywords before the interface methods.
  • Static, final and public keywords before data members.
In other words, methods are public and abstract and interface data members are public, static and final.
 

Why to use interface in Java?

 
Basically an interface is a very important keyword in Java and it has the main advantages that are listed below:
  • The interface helps to obtain full abstraction in Java.
  • The interface helps to do loose coupling in Java application.
  • The interface helps to obtain multiple inheritances in Java.
 interface in Java
 

 

Class-Interface relationship

 
An interface has an abstract method in it and cannot be instantiated. To make an interface useful one should do the following:
  • Interface itself must be implemented by a non-abstract class.
  • A child interface in the last hierarchy must be implemented by a non-abstract class.
  • If an abstract class implements the interface then the abstract class itself must be extended by a non-abstract class.
A class can extend another class, an interface can extend another interface and a class can implement an interface.
 

A simple illustration of an interface

 
In this example, the Day interface has only one method and is implented in the class Night.
  1. interface Day {  
  2.  void show();  
  3. }  
  4. class Night implements Day {  
  5.  public void show() {  
  6.   System.out.println("Sun brights in day time");  
  7.  }  
  8.  public static void main(String args[]) {  
  9.   Night n = new Night();  
  10.   n.show();  
  11.  }  
  12. }  
Output
 
Sun brights in daytime
  
The following shows how multiple inheritances can be done with an interface:
  1. interface Apple {  
  2.  void show();  
  3. }  
  4. interface Rose {  
  5.  void display();  
  6. }  
  7. class Category implements Apple, Rose {  
  8.  public void show() {  
  9.   System.out.println("Apple is a fruit");  
  10.  }  
  11.  public void display() {  
  12.   System.out.println("Rose is a flower");  
  13.  }  
  14.  public static void main(String args[]) {  
  15.   Category n = new Category();  
  16.   n.show();  
  17.   n.display();  
  18.  }  
  19. }  
Output
  
Apple is a fruit.
 
Rose is a flower.
 

Another example of an interface

 
In this example, there are two same methods in two different interfaces. While implementing this as a class then there will be ambiguity, but in the case of an interface, it will work properly without any ambiguity. Let's see what happens.
  1. interface Apple {  
  2.  void display();  
  3. }  
  4. interface Pineapple {  
  5.  void display();  
  6. }  
  7. class Category implements Apple, Pineapple {  
  8.  public void display() {  
  9.   System.out.println("Apple and Pineapple are fruits");  
  10.  }  
  11.  public static void main(String args[]) {  
  12.   Category n = new Category();  
  13.   n.display();  
  14.  }  
  15. }  
Output
 
Apple and Pineapple are fruits.
  

The interface extends interface or nested interface

 
An interface can extend any number of other interfaces.
  1. interface Maths {  
  2.  void display();  
  3. }  
  4. interface Geometry extends Maths {  
  5.  void show();  
  6. }  
  7. interface Trigonometry extends Geometry {  
  8.  void print();  
  9. }  
  10. class Science implements Trigonometry {  
  11.  public void display() {  
  12.   System.out.println("Math is a subject");  
  13.  }  
  14.  public void show() {  
  15.   System.out.println("Geometry is a sub-part of maths");  
  16.  }  
  17.  public void print() {  
  18.   System.out.println("Trigonometry is a sub-part of maths");  
  19.  }  
  20.  public static void main(String args[]) {  
  21.   Science G = new Science();  
  22.   G.display();  
  23.   G.show();  
  24.   G.show();  
  25.  }  
  26. }  
Output
 
Math is a subject
 
Geometry is a sub-part of maths
 
Trigonometry is a sub-part of maths

 
Interface inside a class

 
It is possible to declare an interface inside a class, in this case, the nested interface can be accessed by the outer class name and are declared public static by default.
  1. public class Animal {  
  2.  interface Dog {  
  3.   public void bark();  
  4.  }  
  5. }  
  6. class Name implements Animal.Dog {  
  7.  public void bark() {  
  8.   System.out.println("Barking dog seldom bite");  
  9.  }  
  10.  public static void main(String args[]) {  
  11.   Name N = new Name();  
  12.   N.bark();  
  13.  }  
  14. }  
Output
 
A barking dog seldom bites
 

A class inside an interface

  1. public interface Animal {  
  2.  public class Flower {  
  3.   interface Dog {  
  4.    public void bark();  
  5.   }  
  6.  }  
  7. }  
  8. class Name implements Animal.Flower.Dog {  
  9.  public void bark() {  
  10.   System.out.println("Barking dog seldom bite and rose is a flower");  
  11.  }  
  12.  public static void main(String args[]) {  
  13.   Name N = new Name();  
  14.   N.bark();  
  15.  }  
  16. }   
Output
 
A barking dog seldom bite and rose is a flower
 

Marker or tagged interface in Java

 
An interface that has no data members or method in it is known as a marker or tagged interface such as cloneable, Serializable, Remote and so on.
  
They are just used as a marker that tells JVM to perform some useful and important tasks.
  
How this interface is written:
  1. public interface Cloneable {}   


Similar Articles