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
4
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.
1
u/tehleel_mir_official Nov 09 '18
yes man i am currently reading Java The Complete Reference book and today was this my topic, i understand all i mean Syntex and other cases ....but but but one question still we we need them....what is the benefit of creating interface when we have abstraction inheritance and simple classes .why why why....
2
u/spikebaylor Nov 09 '18
what is the benefit of creating interface when we have abstraction inheritance and simple classes.
There are a few answers in general but the important one is that java does not allow multiple inheritance. Meaning i cant extend AbstractThingA and also AbstractThingB. I can however extend AbstractThingA and also Implement ThingBInterface and ThingCInterface.
1
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 Comparable interface:
The interface declares exactly one method: compareTo
Any class implementing this interface must implement the compareTo method.
Any class/method working with objects that are instances of classes that implement the Comparable interface can rely on the compareTo method being available.
In addition to forming a binding contract, Interfaces can act as super data type since they have the Interface in common.
3
u/tanenbaum Nov 09 '18
To expand: An interface defines a type and implementing it in a class means that the class will have that type in addition to the type it defines itself, as every class does. So you can use the interface as a type when you declare variables or method parameters, but you can never provide the actual interface; it doesn't exist as an object. You provide instantiations of the classes that implement the given interface. In Java, you can declare a variable as a List. However, you can't call new List() on the right side of the declaration as there is no List class; there is only a List interface. You can instead instantiate a variable of the type List as an ArrayList or a LinkedList or something else as these classes implements the List interface.
1
Nov 10 '18
Why would you do this?
List list = new ArrayList();
Why not just do this instead?
ArrayList list = new ArrayList();
The latter looks cleaner than the former. I don’t understand the point of instantiating a variable whose type is an interface. Why not use the desired class instead?
3
u/Civil_Code Nov 10 '18
There's nothing wrong with that, but in the event you decide to change what collection you want to use you'll have an easier time implementing another List type with the former. At the same time, using List as your reference type means the instantiated object will only have access to the methods that are defined in that type, meaning you can restrict access to some methods in a subclass which aren't known to the parent class.
Mostly it helps simplify messaging where you can pass around the most vague reference type possible to reduce repetitive code
2
u/excitedmelon Nov 09 '18
After 2.5 years in comp science and I just honest to good understood what interface meant (similar to OP, knew what it was in theory, but never really had to use it practically) Thank you!
1
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.
5
u/OffbeatDrizzle Nov 09 '18
That's weird, all of your implementations have randomlyDeductAndChargeOverdraftFee functionality
3
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
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
2
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.