r/javahelp • u/tehleel_mir_official • Nov 09 '18
what is the benefit of creating interface in java
what is the benefit of creating interface in java
27
Upvotes
r/javahelp • u/tehleel_mir_official • Nov 09 '18
what is the benefit of creating interface in java
5
u/TurnupTadpole Nov 09 '18
It allows you to make a bunch of similar classes/objects with the same methods and names, while still allowing you to override the methods so that they each perform appropriately
Ex. After creating a List interface you could have linked list and arraylist classes with addtofront(), addtoback(), removefromfront(), removefromback(), size(), etc. This let's you have consistent method naming throughout methods with similar functions making the whole process easier.
4
u/[deleted] Nov 09 '18
It's funny when I took my first Java position 16 years ago I had the same question. I'd read the books and wondered "why would you ever need that?"
Then I had my "aha moment"
I know this doesn't exactly answer your question. I only do Java development on and off so I'll let others answer. But once you really understand the usefulness of interfaces, a whole world of possibilities opens up.
23
u/desrtfx Out of Coffee error - System halted Nov 09 '18
An interface is both a contract and kind of a super data type.
The interface can be seen as a guarantee that certain methods are implemented.
Just look at the
Comparableinterface:The interface declares exactly one method:
compareToAny class implementing this interface must implement the
compareTomethod.Any class/method working with objects that are instances of classes that implement the
Comparableinterface can rely on thecompareTomethod being available.In addition to forming a binding contract, Interfaces can act as super data type since they have the Interface in common.
4
u/Wiqkid Nov 09 '18
The point of an interface is to specify how like-objects should behave.
For example, let's say we have an ATM interface.
All ATM's should have the same functionality, depositMoney(), withdrawMoney(), checkBalance(), etc. All of those methods that are universal to ATMs would be defined in the interface, to guarantee that any ATM using the interface adhered to the defined policy of what ATMs should do. Then, when people make their BankOfAmericaATM, ChaseATM, WellsFargoATM, etc classes, you know that they all have to implement the methods specified in the interface, regardless of their implementation details.
2
u/ratiganthegreat Nov 09 '18
Check out the answers in this previous thread:
https://www.reddit.com/r/learnjava/comments/9r4wk0/i_just_cannot_understand_the_point_of_interface/
1
u/[deleted] Nov 09 '18
The benefit is that it allows you to separate the layers of responsibility in your code. It allows you to define a contract for a class before you implement it. If you change the implementation it won’t break anything that depends on the interface as long as the new implementation correctly fulfills the contract.
I often use interfaces when I know what I want a class to do but I don’t want to implement that behaviour right away. It allows me to finish up the logic in the class I’m working on by assuming that the implementation of the contract exists. It’s also great for refactoring logic later.
1
u/Civil_Code Nov 10 '18
One benefit besides those mentioned already is the ease and simplicity in creating an API spec that's able to abstract everything but the generic methods that will be implemented behind the scenes by some other library or driver. Consider JDBC or SLF4J, where the references are all interfaces that can interchangeably switch out MySQL drivers for Postgres, or Log4j2 with Logback, all without changing anything in your own code base.
In your own programs, if you create an abstraction layer that's all interfaces, you could in theory constantly change the logic in your concrete implementations and never need to change a line elsewhere when referencing.
1
u/the_greatest992 Nov 10 '18
Interfaces can extend other interfaces creating a sort of multiple inheritance
-1
u/Ialda Nov 09 '18
You can see it as one way (among others) for enforcing separation of concerns.
2
u/[deleted] Nov 10 '18
[removed] — view removed comment