What is an interface and what are the advantages of making use of them in Java?

aditya chaudhari
JavaDeveloperDiary — JDD
2 min readOct 20, 2021
interface in java is like a vending machine.

Do you know how soda machine works ? If yes then its easy to understand what is interface in java .

In OOP our code revolves around , Class, Interface, Object, Encapsulation, Abstraction, polymorphism concepts.

In this Abstraction is to hide the actual implementation details, and polymorphism is appear in many forms.
Interface is enabler to achieve abstraction as well as polymorphism.

But, how its related to vending machine ?
Do you know how to use vending machine ? what vending machine do ?
1) accepts coins and option input
2) check if coins are sufficient to accept order
if yes then proceed and return change if required
if no return coins
3) drop the drink in container .

Here for any vending machine above 3 steps are common right ? So in order to use any vending machine user must know how to use it using 3 steps above ( abstraction achieved ) and same steps can be use to build any similar vending machine. Like Soda-vending machine, chocolate vending machine, coca-cola vending machine ( polymorphism achieved)

Interface in java can contain constant, abstract methods , static methods and default methods.

Advantages of using interface :
1. Allows decoupling of a contract and its implementation.
2. Allows for modularization of java program.
3. enable to inject runtime implementation
4. Defines a contract which implementation class must fulfil
3. Increase testability .

An interface is an abstraction that manages complexity by defining how to interact with a process while hiding how the process actually gets done.

Thanks.

references : https://stackoverflow.com/questions/17685992/what-is-the-advantage-of-using-interfaces
https://www.dineshonjava.com/what-is-interface-and-what-are-the-advantages-of-making-use-of-them-in-java/

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT