Open In App

Interfaces in Java

Last Updated : 17 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface contains static constants and abstract methods.

What are Interfaces in Java?

The interface in Java is a mechanism to achieve abstraction. Traditionally, an interface could only have abstract methods (methods without a body) and public, static, and final variables by default. It is used to achieve abstraction and multiple inheritances in Java. In other words, interfaces primarily define methods that other classes must implement. Java Interface also represents the IS-A relationship.

In Java, the abstract keyword applies only to classes and methods, indicating that they cannot be instantiated directly and must be implemented.

When we decide on a type of entity by its behavior and not via attribute we should define it as an interface.

Features of Interfaces

Syntax for Java Interfaces

interface {
// declare constant fields
// declare methods that abstract
// by default.
}

To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement the interface, use the implements keyword.

Uses of Interfaces in Java

Uses of Interfaces in Java are mentioned below:

  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
  • Any class can extend only one class, but can implement multiple interfaces.
  • It is also used to achieve loose coupling.
  • Interfaces are used to implement abstraction. 

So, the question arises why use interfaces when we have abstract classes?

The reason is, abstract classes may contain non-final variables, whereas variables in the interface are final, public, and static.

// A simple interface
interface Player
{
final int id = 10;
int move();
}

Relationship Between Class and Interface

A class can extend another class, and similarly, an interface can extend another interface. However, only a class can implement an interface, and the reverse (an interface implementing a class) is not allowed.

Relationship between Class and Interface

Difference Between Class and Interface

Although Class and Interface seem the same there have certain differences between Classes and Interface. The major differences between a class and an interface are mentioned below:

Class

Interface

In class, you can instantiate variables and create an object.In an interface, you must initialize variables as they are final but you can’t create an object.                       
A class can contain concrete (with implementation) methodsThe interface cannot contain concrete (with implementation) methods.
The access specifiers used with classes are private, protected, and public.In Interface only one specifier is used- Public.

Implementation: To implement an interface, we use the keyword implements

Java
// Java program to demonstrate working of
// interface

import java.io.*;

// A simple interface
interface In1 {
  
    // public, static and final
    final int a = 10;

    // public and abstract
    void display();
}

// A class that implements the interface.
class TestClass implements In1 {
  
    // Implementing the capabilities of
    // interface.
    public void display(){ 
      System.out.println("Geek"); 
    }

    // Driver Code
    public static void main(String[] args)
    {
        TestClass t = new TestClass();
        t.display();
        System.out.println(t.a);
    }
}

Output
Geek
10

Java Interfaces Examples

Let’s consider the example of vehicles like bicycles, cars, bikes, etc they have common functionalities. So we make an interface and put all these common functionalities. And lets Bicycle, Bike, car, etc implement all these functionalities in their own class in their own way.

Below is the implementation of the above topic:

Java