Understanding Interface in Java

1. Introduction

An interface is considered as the core part of Java Programming Language. An interface is similar to a class, but is NOT a class. We can say an interface to be a blueprint of a class with just the static constants and abstract methods. If you are not aware of an abstract method, it is a method without a body.

For example, a class implementing an interface must provide implementation for all of the methods declared in the interface, as an interface consists of only the method definitions, with any body (abstract methods).

2. Why do we need Interfaces?

Interface cannot be instantiated it can only be implemented by different classes, and extended by other interfaces.


It provides a way to achieve full abstraction (a process of hiding details and exposing only the essential features of concept or functionality), like the Facebook API, Twitter API, that exposes just the contract with method names, which their implementations are unknown to the users/developers.

Interface also allows the implementation of multiple inheritance in Java.

3. Declaring an Interface

  • Interface in java is declared using keyword interface.
  • A class uses implements keyword to implement an interface.
  • An interface use extends keyword to extend another interface in its own.

3.1 Diagrammatic Representation

Interface Diagram

3.2 Syntax

In an interface all the methods must be abstract.

3.3 Example

In the above snippet, School is an Interface. We defined an abstract method named as getSchoolName() in it. The class Student implements the interface School, thereby providing a specific implementation to the abstract method (getSchoolName() ) defined in the parent interface.

4. Interface extending another Interface

An interface can extend more than one interface.

4.1 Syntax

4.1 Diagrammatic Representation

figure2extends

4.2 Example

In the above example, we declared two interfaces Person and Student.
Student interface extends Person interface, which means that the Student interface has two abstract methods (eats() and study()). One from its own interface and another one inherited from the Person interface. Class Program implements both Person and Student interfaces, thereby has to provide implementation to both the abstract methods, eats() and study().

5. Interfaces are a good substitute for multiple inheritance, how?

Yes, it’s true. Interfaces are a good substitute to achieve multiple inheritance.

As child class can’t extend more than one parent class, but an interface makes multiple inheritance possible. A class can extend multiple interfaces, and there is no ambiguity while extending more than one interfaces in the same class as implementation is provided by the only implementation class.

Multiple Inheritance

6. What does it mean to – “program to an interface instead of an implementation”?

“Programming to an interface” means, that when possible, one should refer to a more abstract level of a class (an interface, abstract class, or sometimes a superclass of some sort), instead of referring to a concrete implementation. At an abstract level, we are concerned about what is being done, rather than how is it done. Assume that if the how process changes tomorrow for whatever reasons, such that instead of an ArrayList instance, a LinkedList instance is returned. Had we depended upon a particular implementation class instance, say ArrayList, to be returned from the API and the API starts to return an instance of another implementation class, say LinkedList, believe me, it would have ripped off our application. But if we are ready to accept any implementation class instance by accepting a common interface type, say a List (which is a common interface to all the List implementations), we are completely safe from such disasters.

  • DHIRENDRA GOHIL

    What does it mean to – “program to an interface instead of an implementation”?
    liked this, very good explanation..
    Thanks

    • Its really a wonderful feeling to know that you liked it. Thanks for spending your time reading this and do refer it across your friends. Thanks a lot!

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.