Interface in JAVA

Interface in JAVA

Interface is like a template of the class. An interface contains only static constants and method declarations that is methods with no body, that is why interface is also a blueprint of the class. Hence, we can say that all methods in an interface are abstract.

An interface guarantees that the methods listed will be implemented.

To declare an interface use keyword interface instead of class.

In an interface all the methods declared are by default public, hence there is no need to specify the keyword public.

The multiple inheritance problem in JAVA is solved by implementing interfaces.

Note :- We cannot create an instance of an interface.

Why do we need interfaces?

Let us take an example, we have two clients c1 and c2, so obviously  when we want to distribute our application to them, their needs will be different so we will have to make different contracts for both the clients. Hence I create one interface say I1(contract 1) which has the method m1() needed by client 1 and another interface I2(contract 2) which has the method m2() needed by client 2. Let us now implement these interfaces, we create a class c1 which will implement interface I1 and create a class c2 which implements interface I2, hence now we can distribute the software to client 1 by using contract 1 and for client 2 we can use contract c2.

Through interface we can achieve :-

  • Abstraction
  • Loose Coupling
  • Multiple Inheritance

A program for showing the use of interface in java

Program (Save as Main.java)

public class Main{

    public static void main(String []args){
        Shape c = new Circle();
        Shape s = new Square();
        c.area(4);
        c.display();
        s.area(5);
        s.display();
        s.area(7);
        s.display();
    }
}

interface Shape{
    void area(int x);
    void display();
    public static final double pi = 3.142;
}

class Circle implements Shape{
    double a = 1;
    public void area(int x){
        a = pi*x*x; 
    }
    public void display(){
        System.out.println("Area of Circle = "+a);
    }
}

class Square implements Shape{
    double a = 1;
    public void area(int x){
        a = x*x;  
    }
    public void display(){
        System.out.println("Area of Square = "+(int)a);
    }
}

Output

Area of Circle = 50.272
Area of Circle = 25
Area of Circle = 49

When to use Interface and Abstract Class?

Interface must be used when we want to implement multiple features from different classes. Let us say we have two interfaces I1 and I2 and each interface has methods m1() and m2() respectively and they are defined in class c1 and c2 respectively. Now we have another class c3 which needs features defined in both methods m1() and m2(), then we simply implement interfaces I1 and I2.

Abstract classes must be used when you want to provide some common base code but want the user's of your class to complete the implementation. Hence use abstract class when there is common behaviour.

Share Me!
  •  
  •  
  •  
  •  
  •  
  •