Java Interface
Java Interface
Java Interface
In this tutorial, we will learn about Java interfaces. We will learn how to
implement interfaces and when to use them in detail with the help of
examples.
interface Language {
public void getType();
Here,
Language is an interface.
It includes abstract methods: getType() and getVersion() .
Implementing an Interface
Like abstract classes, we cannot create objects of interfaces.
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
Run Code
Output
Here, the Rectangle class implements Polygon . And, provides the implementation
of the getArea() method.
class Main {
public static void main(String[] args) {
ProgrammingLanguage language = new ProgrammingLanguage();
language.getName("Java");
}
}
Run Code
Output
Here, the ProgrammingLanguage class implements the interface and provides the
implementation for the method.
interface A {
// members of A
}
interface B {
// members of B
}
class C implements A, B {
// abstract members of A
// abstract members of B
}
Extending an Interface
Similar to classes, interfaces can extend other interfaces. The extends keyword
is used for extending interfaces. For example,
interface Line {
// members of Line interface
}
// extending interface
interface Polygon extends Line {
// members of Polygon interface
// members of Line interface
}
Here, the Polygon interface extends the Line interface. Now, if any class
implements Polygon , it should provide implementations for all the abstract
methods of both Line and Polygon .
Extending Multiple Interfaces
interface A {
...
}
interface B {
...
}
interface C extends A, B {
...
}
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 provide specifications that a class (which implements it)
must follow.
In our previous 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.
Now any class that implements the Polygon interface must provide an
implementation for the getArea() method.
Interfaces are also used to achieve multiple inheritance in Java. For
example,
interface Line {
…
}
interface Polygon {
…
}
class Rectangle implements Line, Polygon {
…
interface Language {
To declare default methods inside interfaces, we use the default keyword. For
example,
// default method
default void getSides() {
System.out.println("I can get sides of a polygon.");
}
}
class Main {
public static void main(String[] args) {
Output
Here, we have created two classes Rectangle and Square that implement Polygon .
The Rectangle class provides the implementation of the getArea() method and
overrides the getSides() method. However, the Square class only provides the
implementation of the getArea() method.
Now, while calling the getSides() method using the Rectangle object, the
overridden method is called. However, in the case of the Square object, the
default method is called.
// create an interface
interface Polygon {
staticMethod(){..}
}
Note: With the release of Java 9, private methods are also supported in
interfaces.
We cannot create objects of an interface. Hence, private methods are used as
helper methods that provide support to other methods in interfaces.
interface Polygon {
void getArea();
class Main {
public static void main(String[] args) {
Triangle t1 = new Triangle(2, 3, 4);
// calls the method of the Triangle class
t1.getArea();
Output
Area: 2.9047375096555625
Perimeter: 9
Now, all polygons that implement Polygon can use getPerimeter() to calculate
perimeter.
However, the rule for calculating the area is different for different polygons.
Hence, getArea() is included without implementation.
Any class that implements Polygon must provide an implementation of getArea() .
REFERENCES: https://www.programiz.com/java-programming/interfaces