+ 3

Help to understand interfaces

So, as the title suggests, I need help understanding interfaces. What's the main appeal or purpose for them? I've seen a couple tutorial videos, and even read the section in my Java text. But, I am having trouble understanding the point of it. It may sound like a naive question, but hear me out: The interface seems to standardize method calling, instead of writing several of the same methods for all your classes to achieve the same result. But you still end up writing the code for each method uniquely in all the classes that implement the interface. What is the difference exactly? The book said that interfaces normally don't contain code, just the method calls. So I'm having difficulty understanding the whole concept of this. Any help would be appreciated!

5th Nov 2017, 1:20 PM
Sapphire
9 Answers
+ 5
too simplify and standardize methodcalls. Collections.sort for example can sort any object as long as you implement the Comparable interface. Collection.sort doesnt know what objects it is sorting but thanks to the interface it knows how to sort them. you use interfaces when you need different objects to do the same thing but each object behaves a bit differently. If you code in a an real IDE (not the code playground) and implement an interface it automatically implements the missing methods for you. whats also neat: imagine a Car class and a Bike class, both implementing an interface called Moveable. To store multiple Cars and Bike objects you would normally need 2 arrays, but since they both implement Movable you can simply create a Movable array and store Cars and Bikes in it. Example: https://code.sololearn.com/cpV2V0MVmBny/?ref=app
5th Nov 2017, 1:38 PM
Jeremy
Jeremy - avatar
+ 4
Think of it like a contract, if you use the Interfaces name within a class, then you can pass any class to use in the interfaces place, as long as that class implements the interface. This means I don't have to recompile the class each time I want to change the behavior of the passed class and its functions.
5th Nov 2017, 2:23 PM
josh mizzi
josh mizzi - avatar
+ 1
you cannot instantiate Objects of the the interfaces type but you can use the interfaces type to store any object whichs class implemented that interface. It works by simply using the Interfaces name as datatype and then instead of "new NameOfInterfave()" you do "new ClassThatImplementsThatInterface()" And yes you can use the interfaces type in arraylists and otther sorts of list or generics. Another thing: use interfaces if one of you class HAS TO to use a certain method call to do it's task. the only way to know if the objects has that method is if it implemented the correct interface. (Collections.sort for example relies on the compare() method of the Comparable interface)
5th Nov 2017, 2:10 PM
Jeremy
Jeremy - avatar
+ 1
Thats correct. You can practice interfaces by playing around with the Comparable interface and Collections.sort with your own object to get the hang of it. Other interface with which you can practice are Cloneable, Runnable and Iterable. Whats also cool are functional Interfaces and lambda.
5th Nov 2017, 2:26 PM
Jeremy
Jeremy - avatar
+ 1
functional interfaces are interfaces with only one method, these can be implemented via lambda or method reference. i have some examples here: https://code.sololearn.com/cc7DMRVGTTh9/?ref=app
5th Nov 2017, 2:40 PM
Jeremy
Jeremy - avatar
+ 1
Thanks for the example codes. I'll get used to the interface after awhile of usage. Thanks again for help in understanding.
5th Nov 2017, 3:16 PM
Sapphire
0
Thanks for your response! I sort of understand it, but I guess I'd need to play around with it more to comprehend it's significance. 1) I did not realize that interfaces could initialize code other than method declarations. How does that work? 2) Since you'd be calling an interface when creating a new object of a class, does that mean an ArrayList (or any other type of container) datatype of the interface can store all class objects that implemented it?
5th Nov 2017, 2:06 PM
Sapphire
0
Ah, now I see. So the car/bike example was really just an array that used the interface "Movable" as a datatype to store both. Is that a correction assumption? Alright, I'm starting to see the importance of interfaces now, or at least some use to them.
5th Nov 2017, 2:22 PM
Sapphire
0
@josh That makes a lot more sense. So a good analogy to this is several companies (classes) that design parts for cars, trucks, etc all under a single Brand name (interface). The Brand standardizes the making of some parts that all vehicles use (like wheels), but it's up to the companies' to design them? @Jeremy I'm already familiar with lambda anonymous functions (from Python at least), not yet familiar with "functional interfaces". How are those different?
5th Nov 2017, 2:36 PM
Sapphire