What is the purpose of interfaces in java?



An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.

To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.

All the methods of the interface are public and abstract and, we will define an interface using the interface keyword as shown below −

interface MyInterface{
   public void display();
   public void setName(String name);
   public void setAge(int age);
}

Purpose of the interface

  • Provides communication − One of the uses of the interface is to provide communication. Through interface you can specify how you want the methods and fields of a particular type.
  • Multiple inheritance  − Java doesn’t support multiple inheritance, using interfaces you can achieve multiple inheritance −

Example

 Live Demo

interface MyInterface1{
   public static int num = 100;
   public default void display() {
      System.out.println("display method of MyInterface1");
   }
}
interface MyInterface2{
   public static int num = 1000;
   public default void display() {
      System.out.println("display method of MyInterface2");
   }
}
public class InterfaceExample implements MyInterface1, MyInterface2{
   public void display() {
      System.out.println("This is the implementation of the display method");
   }
   public void show() {
      MyInterface1.super.display();
      MyInterface2.super.display();
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.show();
   }
}

Output

display method of MyInterface1
display method of MyInterface2
  • Abstraction − Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.

    Since all the methods of the interface are abstract and user doesn’t know how a method is written except the method signature/prototype. Using interfaces, you can achieve (complete) abstraction.

  • Loose coupling − Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.

    You can achieve loose coupling in Java using interfaces −

Example

 Live Demo

interface Animal {
   void child();
}
class Cat implements Animal {
   public void child() {
      System.out.println("kitten");
   }
}
class Dog implements Animal {
   public void child() {
      System.out.println("puppy");
   }
}
public class LooseCoupling{
   public static void main(String args[]) {
      Animal obj = new Cat();
      obj.child();
   }
}

Output

kitten

Advertisements