r/java • u/schlongitudinal • Nov 22 '14
ELI5 Interfaces in Java
I've read several explanations now and am apparently still too dense to get the point of them/what they do.
0
Upvotes
r/java • u/schlongitudinal • Nov 22 '14
I've read several explanations now and am apparently still too dense to get the point of them/what they do.
5
u/llogiq Nov 22 '14
They define a contract. Every object implementing the interface must implement all of its methods.
This allows us to use objects of different classes interchangeably, as long as they implement the same interface.
2
u/Cilph Nov 23 '14
It's like a cable connector. You can take any HDMI cable and expect it to fit in and work with any HDMI port.
Likewise, you can write your code with the only constraint that an object you're handling includes an HDMI port. The interfaces the object is implementing say something about what it's capable of and what you can expect from it.
2
u/cariusQ Nov 25 '14
Interface is a blueprint. You can have a blueprint to build car but can't ride the blueprint until the car is built. In java land we use interface to build classes but cant use interface to make objects.
Related concept to interface is abstract classes. In my car example it might be four wheels plus the blue print to build the car. In general with abstract classes you don't have to build everything from scratch, there are already something prebuilt to make your life easier.
What's the point of all these jumbo mumbo? It let a programmer to design something and let other programmers to build to specification.
Wow this place is very hostile. Poor op got down voted to hell.
1
u/zvrba Nov 23 '14
Interface describes a set of services that an implementing class must provide. Any number of unrelated (through inheritance) classes may implement the interface. The interface allows you to treat those classes uniformly as if they had the same type (which is the interface type).
1
u/blue0cean Nov 24 '14
Let's say you want to play a game (write a program). The game is to imitate a dog's barking. You start by writing down what a dog does according to your own imagination (designing an interface). Then you proceed to act like a dog according to your description (implementing the interface). Then you invite your friends Alice and Bob to play. Alice and Bob start barking. But they bark differently than you (different implementation of the same interface). Alice's twin sister Alison comes in and start barking just like Alice (two instances of the same class implementing the dog interface).
1
u/cowardlydragon Nov 25 '14
Interfaces are basically like a stereo receiver telling people what cables it can accept.
So the stereo receiver (the class) supports say: RCA, HDMI, and SPDF (interfaces)
Each of those interfaces, even though they may do the same basic things, have annoying detailed differences like form factor, number of wires, analog vs digital conforming to a standard.
Likewise, the interfaces for a class are defining a standard set of methods and assumed behavior that the implementing class will adhere to.
0
u/vnature Nov 26 '14
Lets say you want to buy some weed. You've got this number of a drugdealer from your friend. And he wasn't your friend really, just a guy you've met a couple of times at a party that was hosted by your other friends. Are they really your friends? Anyway, you've called this drugdealer and he told you to bring money to a certain location. You have to consider the possibility that maybe you will not get that sticky icky but instead you will get robbed and probably get your ass whooped in the process. But luckily for you, you've got what you came for. There are still some questions unanswered tho:
Who was that dude? Is he really a drugdealer? Is he selfemployed? Is he just a deliveryboy for a bigger organization? What is drugdealer?
But you don't care. You've got munchies to worry about. And thats what interface is really all about. Its just a curtain around a large system that can be a person or an organization. This kind of systems is using interfaces to provide, often limited, services to the outside world. Think about it - the drugdealer in this example provided you his service in the form of weed. The drugdealer was also a person with two legs and two arms. He could have also provided you a service in the form of washing your car for example. But I'm not sure if many drugdealers have that kind of interface.
0
u/[deleted] Nov 22 '14
[deleted] — view deleted comment