Prerequisites: Java Basics Part 2

Let's learn about the concept of Java Interfaces with the help of a few examples in this lesson.

Java Interface

An interface is a fully abstract class that includes a group of methods without a body.

In Java, an interface defines a set of specifications that other classes must implement. For example:

interface Language {
   public void getName();
}

Here, we used the interface keyword to create an interface named Language. The Language interface defines the specification getName().

Now, every class that uses this interface should implement the getName() specification.

Press + to interact
// create an interface
interface Language {
void getName(String name);
}
// class implements interface
class ProgrammingLanguage implements Language {
// implementation of abstract method
public void getName(String name) {
System.out.println("Programming Language: " + name);
}
}
class Main {
public static void main(String[] args) {
ProgrammingLanguage language = new ProgrammingLanguage();
language.getName("Java");
}
}

In the above example, we created an interface named Language. The interface includes an abstract method called getName().

Here, the ProgrammingLanguage class implements the interface and provides the implementation for the method.

It is not compulsory to use the abstract keyword while declaring abstract methods inside an interface, because interfaces only include abstract methods, not regular methods.

Note: All the methods inside an interface are implicitly public and all fields are implicitly public static final: For example,

interface Language {
  
  // by default public static final
  String type = "programming language";

  // by default public
  void getName();
}

Implementing an Interface

Like abstract classes, we cannot create objects of an interface. However, we can implement an interface.

We use the implements keyword to implement an interface. For example:

Press + to interact
// create an interface
interface Polygon {
void getArea(int length, int breadth);
}
// implement the Polygon interface
class Rectangle implements Polygon {
// implementation of abstract method
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) {
// create an object
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}

In the above example, we created an interface named Polygon. The interface contains an abstract method getArea().

Here, the Rectangle class implements Polygon and provides the implementation of the getArea() method.

A class can implement multiple interfaces. For example:

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

Like classes, interfaces can extend other interfaces. The extends keyword is used to extend interfaces. For example:

interface Line {
  // members of Line interface
}

// extending interface
interface Polygon extends Line {
  // members of Polygon interface
  // members of Line interface
}

In the above example, 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.

An interface can extend multiple interfaces. For example:

interface A {
   ...
}
interface B {
   ... 
}

interface C extends A, B {
   ...
}

Practical example of an interface

Let’s see a more practical example of Java Interface.

Press + to interact
// To use the sqrt function
import java.lang.Math;
interface Polygon {
void getArea();
// calculate the perimeter of a Polygon
default void getPerimeter(int... sides) {
int perimeter = 0;
for (int side: sides) {
perimeter += side;
}
System.out.println("Perimeter: " + perimeter);
}
}
class Triangle implements Polygon {
private int a, b, c;
private double s, area;
// initializing sides of a triangle
Triangle(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
s = 0;
}
// calculate the area of a triangle
public void getArea() {
s = (double) (a + b + c)/2;
area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area: " + area);
}
}
class Main {
public static void main(String[] args) {
Triangle t1 = new Triangle(2, 3, 4);
// calls the method of the Triangle class
t1.getArea();
// calls the method of Polygon
t1.getPerimeter(2, 3, 4);
}
}

In the above program, we created an interface named Polygon. It includes a default method getPerimeter() and an abstract method getArea().

We are able to calculate the perimeter of all polygons in the same manner by implementing the body of getPerimeter() in Polygon.

Now, all polygons that implement Polygoncan use getPerimeter() to calculate perimeter.

However, the rule for calculating the area is different for different polygons. Thus, getArea() is included without implementation.

Any class that implements Polygon must provide an implementation of getArea().

Check your knowledge!

1

Which of the following is true about methods in an interface in Java?

A)

An interface can contain only abstract methods.

B)

We can define a method in an interface.

C)

The private and protected access modifiers can also be used to declare methods in an interface.

D)

None

Question 1 of 20 attempted