Interface

Yugandharkumar
3 min readMar 10, 2023

--

Interface is a very important feature of Java whose definition starts with the keyword interface. It is very much like a class with some differences. Basically through interface keyword you can declare and define an interface in java. An interface basically specifies method declaration and contains fields or variables.

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).

  • Interfaces specify what a class must do and not how. It is the blueprint of the behaviour.
  • Interface do not have constructor.
  • Represent behaviour as interface unless every sub-type of the class is guarantee to have that behaviour.
  • An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
  • A Java library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection.

Syntax:

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 interface use implements keyword.

Why do we use an Interface?

  • 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 1 class but can any class implement infinite number of interface.
  • 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();
}

Advantages of Interfaces in Java

The advantages of using interfaces in Java are as follows:

  1. Without bothering about the implementation part, we can achieve the security of the implementation.
  2. In Java, multiple inheritances is not allowed, however, you can use an interface to make use of it as you can implement more than one interface.

Important Points About Interface or Summary of the Article:

  • We can’t create an instance(interface can’t be instantiated) of the interface but we can make the reference of it that refers to the Object of its implementing class.
  • A class can implement more than one interface.
  • An interface can extend to another interface or interface (more than one interface).
  • A class that implements the interface must implement all the methods in the interface.
  • All the methods are public and abstract. And all the fields are public, static, and final.
  • It is used to achieve multiple inheritances.
  • It is used to achieve loose coupling.
  • Inside the Interface not possible to declare instance variables because by default variables are public static final.
  • Inside the Interface constructors are not allowed.
  • Inside the interface main method is not allowed.
  • Inside the interface static ,final,private methods declaration are not possible.

--

--

Yugandharkumar
Yugandharkumar

Written by Yugandharkumar

Android DEV skilled in Kotlin, Jetpack Compose, and MVVM. Passionate about building intuitive apps and exploring new tech. Let’s create something amazing! 🚀

No responses yet