Java Interface
Java Interface
void getArea();
}
implements Keyword in Interface
Like abstract classes, we cannot create objects of interfaces. However,
we can implement interfaces in other classes. In Java, we use the
implements keyword to implement interfaces. For example,
interface Polygon
{
void getArea(int length, int breadth);
}
class Rectangle implements Polygon
{
public void getArea(int length, int breadth)
{
System.out.println("The area of the rectangle is " + (length
* breadth));
}
}
class Main
{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
Output
The area of the rectangle is 30
In the above program, we have created an interface Polygon. The
Polygon interface has an abstract method getArea().
This means that any class that implements Polygon must provide an
implementation for the getArea() method.
Notice that, the Rectangle class (which implements Polygon interface)
has the method getArea() with implementation.
Why use Interfaces?
Now that we know what interfaces are, let’s learn about why interfaces
are used in Java.
Interfaces provide specifications that a class (which implements it) must
follow.
In our above example, we have used getArea() as a specification inside
the interface Polygon. This is like setting a rule that, we should be able
to get the area of every polygon. So any class that implements the
Polygon interface must provide an implementation for the getArea()
method.
Similar to abstract classes, interfaces help us to achieve abstraction in
Java. Here, we know getArea() calculates the area of polygons but the
way area is calculated is different for different polygons. Hence, the
implementation of getArea() is independent of one another.
Interfaces are also used to achieve multiple inheritance in Java. If a
subclass is inherited from two or more classes, it's multiple inheritance.
In Java, multiple inheritance is not possible by extending classes.
However, a class can implement multiple interfaces. This allows us to
get the functionality of multiple inheritance in Java. For example,
interface Line
{
...
}
interface Polygon
{
...
}
class Rectangle implements Line, Polygon{
...
}
Here, Rectangle has to provide an implementation for all methods of
both Line and Polygon.
private and static Methods in Interface
With the release of Java 8, interfaces now can include static methods.
Similar to a class, we can access static methods of an interface using its
references. For example,
Polygon.staticMethod();
Also, interfaces support private methods with the release of Java 9. Now
you can use private methods and private static methods in interfaces.
Since you cannot instantiate interfaces, private methods are used as
helper methods that provide support to other methods in interfaces.
default methods in Interfaces
With the release of Java 8, methods with implementation (default
methods) were introduced inside an interface. Before that, all the
methods were abstract in Java.
To declare default methods inside interfaces, we use the default
keyword. For example,