r/javahelp • u/The_King1337 Intermediate Brewer • Jun 19 '13
When would I use interfaces?
I've recently learned about interfaces, but when would I actually use them? Thanks!
2
Jun 19 '13
I'll use an example involving the Comparable interface. Say you create several objects that all implement Comparable.
You can then create Lists, ArrayLists, etc and ensure that all objects that are contained in that list implement Comparable (and therefore have compareTo, which you use to insert items in an ordered fashion). This would be achieved by saying something akin to:
ArrayList< E extends Comparable<E> > list = new ArrayList<E extends Comparable<E> >();That's the instance that I use it most frequently in. It can be changed so that Comparable is some sort of custom written interface, thereby ensuring that only objects that implement a certain interface belong in a certain list.
2
u/DLFCN Jun 19 '13
I would implement an interface to avoid writing duplicate code/methods in multiple classes. Makes things easier if you decide to change something, you only have to change one method in your interface rather than however many classes have that method in it.
So... For example your making an Alien Space Shooter game. Your PlayerShip.class and EnemyShip.class may implement an interface called Movement that deals with moving an object around screen. The interface would have methods such as moveLeft(), moveUp(), rotate(), etc. that both payer ship class and enemy ship class call often. Interfaces can also store information, for example your player ship and enemy ship will most likely have an X and Y coordinate on screen. In your interface you make and int x and int y, and have your movement methods in the interface manipulate those ints and then retrieve those ints with a get method in your interface.
Some good reading: http://docs.oracle.com/javase/tutorial/java/IandI/index.html
2
u/dudleydidwrong Jun 19 '13
One practical issue is that you can only inherit from one class. Some languages lik C++ allow multiple inheritance, but experience suggested that unrestrained multiple inheritance can take you to hell in a handbasket very quickly. A set of guidelines were developed fo C++ programmers to help keep them out of the deep end of the multiple inheritance cess pool. One of these guidelines said to only inherit data fields from one class; all the other classes should be pure virtual classess. Java formalized this concept to an interface. So in Java you can extend one full calss, but you can use as many interfaces as you want.
I think the best way to learn about interfaces and motivations for using them is to learn about GUI and event-driven programming. Swing uses just s about every feature of classes and interfaces. You may not like Swing for other reasons, but it is useful as a learning tool.
0
u/Stromovik Jun 19 '13
Callbacks
Calling classes with unknown logic , but enforcing the implementation of set of methods. While in theory you could write without interfaces. It will be a nightmare to read.
-2
0
Jun 19 '13
[deleted]
2
u/hadihaha Jun 20 '13
Thankyou! I have just finished a semester of OO Java and only now has it clicked properly!
1
u/AS1LV3RN1NJA Sep 15 '13
Sorry I'm late to the thread (going through saved links).
I'd just like to say, this is probably the clearest explanation I've seen, thanks.
8
u/[deleted] Jun 19 '13
Hey it's you again!
An interface works as a promise. "I'll get you a car, let's work out the details later".
When you actually extend that interface and implement the methods, you need to stick to that promise.
So, when would you use them? Here's an example I did just the other day -
I was creating this simple game, and I needed to create 'things', all the 'things' had to have a sprite mapped to them, they also had to do things when they were hit, or when they hit something.
So I created this interface I called Entity.
An Entity has methods for getSprite(), onCollisionX(), onCollisionY(), gotHit(), hit(), moveY(),moveX(), amongst other things. When I pass around my Entity, it doesn't care if its a bullet, a goomba, a player, because every time I pass it around, those general methods I've promised to be there.
So when I'm resolving some collisions, I can just go through my entities, check if they collided with something, then I don't have to do all this
I can instead just do
And then my individual classes will live up to their promise and resolve their issues in whatever way is best, a bullet will take away HP and disappear when it hits something, and a character will stop moving if it hits something, but all of these are broken into their own instances of the Entity interface, instead of lumped together into a weird pile of logic.
So, I guess in very basic, over simplified terms - you'd use an interface when you want multiple things to have the same types of actions, but respond differently.