DEV Community

Preethi Nandhagopal
Preethi Nandhagopal

Posted on

Interface

Definition of Interface:
An interface is a blueprint of a class that contains abstract methods (without implementation) and constants. It is used to achieve abstraction and multiple inheritance. A class that implements an interface must provide the implementation for all its methods.

Keypoint:
All methods are public and abstract by default (before java 8).
Variables inside interface are public, static and final by default (constants).
A class uses the keyword implements to use an interface.
A class can implement multiple interfaces (solves multiple inheritance issue in java).
From java 8 onwards:
Interface can have default methods (with body).
Interface can have static methods.
From java 9 onwards:
Interface can have private methods (use inside interface only).

Why we use interfaces?
Achieve abstraction->Only method declaration, no implementation.
Multiple inheritance->A class can implement multiple interface.
Loose coupling->Helps in writing flexible and maintainable code.
Polymorphism->One interface, multiple implementations.

Top comments (0)