What is Interface ❓ Interface 🆚 Abstract Class

Anupam Haldkar
3 min readJan 25, 2021

--

hey👋🏻 everyone, In this story you will understand the interface term and how it is different from abstract class.

What is Interface ❓

An interface in Java is a blueprint👣 of a class. It contain static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

In other words🔡, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

What it contains ❓

An interface can contain a subset of what a normal class can contain. This includes everything that is static, both methods and variables, and non-static method declarations. It is not allowed to have non-static variables.
A declaration of a method differs from a normal method in several things; here is one as an example:

[public] [abstract] ReturnType methodName();

These declarations can be marked as public and abstract, as represented with [optional braces]. It is not necessary to do so, as it is the default. private, protected, package-private (aka. nothing) and the final modifier are not allowed and marked as a compiler error. They have not implementation, so there is a semicolon instead of curly braces.

As of Java 8, they can hold non-static methods with an implementation, these have to be marked with the default modifier. However, the same restrictions as to the other modifiers apply (adding that strictfp is now valid and abstract is no more).

What it’s useful for ❓

One of its uses is for it to be used as a face for a service. When two parties work together to form a service-requester & service-provider kind of relationship, the service provider provides the face of the service (as to what the service looks like) in the form of an interface.
One of the OOP concept is “Abstraction” which means to hide away complex working of the systems and show only what is necessary to understand the system. This helps in visualizing the working of a complex system. This can be achieved through interface where in each module is visualized (and also implemented) to work through interface of another module.

Abstract 🆚 Interface

The technical differences between an abstract class and an interface are:

Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.

Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).

When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an abstract class can extend another abstract class and abstract methods from the parent class don’t have to be defined.

Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.

A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.

A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

--

--

Anupam Haldkar
Anupam Haldkar

Written by Anupam Haldkar

Spreading Assist Tech Shaper 🤝 Software Engineer 🧑‍💻| Tech Dev ⚙️

No responses yet