Interfaces in JAVA
In Java, an interface specifies the behaviour of a class by providing an abstract type.
As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported
through this technology.
Interfaces are used in Java to achieve abstraction. By using the implements keyword, a java class can
implement an interface.
In general terms, an interface can be defined as a container that stores the signatures of the methods
to be implemented in the code segment. It improves the levels of Abstraction.
Need for Interface in Java
So we need an Interface in Java for the following reasons:
• Total Abstraction
• Multiple Inheritance
• Loose-Coupling
Total Abstraction
Abstraction is the critical concept of Object-Oriented programming techniques. An interface only
stores the method signature and not the method definition. Method Signatures make an Interface
achieve complete Abstraction by hiding the method implementation from the user.
Multiple Inheritance
Without Interface, the process of multiple inheritances is impossible as the conventional way of
inheriting multiple parent classes results in profound ambiguity. This type of ambiguity is known as
the Diamond problem. Interface resolves this issue.
Loose Coupling
The term Coupling describes the dependency of one class for the other. So, while using an interface,
we define the method separately and the signature separately. This way, all the methods, and classes
are entirely independent and archives Loose Coupling.
Java Interface Syntax
So the Syntax of an Interface in Java is written as shown below.
Interface <Interface Name> {
//Declare Constant Fields;
//Declare Methods;
//Default Methods;
}
With the syntax explained, let us now move ahead onto the next part, where we go through an
example.
Rules for Creating Interfaces
In an interface, we’re allowed to use:
• constants variables
• abstract methods
• static methods
• default methods
What Can We Achieve by Using Them?
Behavioural Functionality
We use interfaces to add certain behavioural functionality that can be used by unrelated classes. For
instance, Comparable, Comparator, and Cloneable are Java interfaces that can be implemented by
unrelated classes.
Multiple Inheritances
Java classes support singular inheritance. However, by using interfaces, we’re also able to implement
multiple inheritances.
Polymorphism
It’s the ability for an object to take different forms during runtime. To be more specific it’s the
execution of the override method that is related to a specific object type at runtime.
In Java, we can achieve polymorphism using interfaces.
Default Methods in Interfaces
to add an abstract method to an existing interface, then all the classes that implement that interface
must override the new abstract method. Otherwise, the code will break.
Java 8 solved this problem by introducing the default method that is optional and can be implemented
at the interface level.
Interface Inheritance Rules
In order to achieve multiple inheritances thru interfaces, we have to remember a few rules. Let’s go
over these in detail.
Interface Extending Another Interface
When an interface extends another interface, it inherits all of that interface’s abstract methods.
Abstract Class Implementing an Interface
When an abstract class implements an interface, it inherits all of its abstract and default methods.
Java Interface Example
Following is an ideal example of Interface in Java. Here we try to calculate the area of geometrical
shapes, and for each shape, we have different methods. And all the methods are defined, independent
of each other. Only method signatures are written in the Interface.
//Interface
package simplilearn;
public interface Area {
public void Square();
public void Circle();
public void Rectangle();
public void Triangle();
}
//Class
package simplilearn;
import java.util.Scanner;
public class shapeArea implements Area {
public void Circle() {
Scanner kb = new Scanner(System.in);
System.out.println("Enter the radius of the circle");
double r = kb.nextInt();
double areaOfCircle = 3.142 * r * r;
System.out.println("Area of the circle is " + areaOfCircle);
}
@Override
public void Square() {
// TODO Auto-generated method stub
Scanner kb2 = new Scanner(System.in);
System.out.println("Enter the length of the side of the square");
double s = kb2.nextInt();
double areaOfSquare = s * s;
System.out.println("Area of the square is " + areaOfSquare);
}
@Override
public void Rectangle() {
// TODO Auto-generated method stub
Scanner kb3 = new Scanner(System.in);
System.out.println("Enter the length of the Rectangle");
double l = kb3.nextInt();
System.out.println("Enter the breadth of the Rectangle");
double b = kb3.nextInt();
double areaOfRectangle = l * b;
System.out.println("Area of the Rectangle is " + areaOfRectangle);
}
@Override
public void Triangle() {
// TODO Auto-generated method stub
Scanner kb4 = new Scanner(System.in);
System.out.println("Enter the base of the Triangle");
double base = kb4.nextInt();
System.out.println("Enter the height of the Triangle");
double h = kb4.nextInt();
double areaOfTriangle = 0.5 * base * h;
System.out.println("Area of the Triangle is " + areaOfTriangle);
}
public static void main(String[] args) {
shapeArea geometry = new shapeArea();
geometry.Circle();
geometry.Square();
geometry.Rectangle();
geometry.Triangle();
}
}
Interface vs Class
Though a class and an Interface look a little similar, they are extremely different from each other.
Some of the primary differences between them are enlisted below as follows.
Interface Class
Keyword used: interface Keyword used: class
Interfaces do not have a constructor Class includes a constructor
Interface stores only the signature of a
Class stores complete method definition
method
Interfaces do not need Access Specifiers In Class, Access Specifiers are mandatory
Interfaces do not include Data Members Class includes Data Members
Interfaces do not have Static Members Class includes Static Members
Disadvantages of Interface in Java
• An interface in real-world projects is used either extensively or not at all.
• The use of Interface can reduce the execution speed.