r/javahelp • Intermediate Brewer • Jun 19 '13

When would I use interfaces?

I've recently learned about interfaces, but when would I actually use them? Thanks!

17 Upvotes

16 comments sorted by

View all comments

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

if( currentEntity.isCharacter() ) {
   // do stuff for when the character hits something
} else if ( currentEntity.isBullet()) {
   // do stuff for when the bullet hits something.
}

I can instead just do

attacker.hit(defender);
defender.gotHit(attacker);

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.

2

u/The_King1337 Intermediate Brewer Jun 19 '13

Thank you, I understand that. But why not just use a class with those methods and override them as necessary?

4

u/[deleted] Jun 19 '13

My reddit seems to be broken or something, nothing is showing up, so hopefully you receive this.

There's no reason you couldn't use a class (at least for simpler examples, more complex examples, an interface helps you avoid the deadly diamond of death - more here http://en.wikipedia.org/wiki/Multiple_inheritance ), but then, why not use assembly, when you can create equally powerful programs? Languages provide features for you to use, you never want someone to instantiate an 'Entity', you want them to use it. There's a certain abstractness behind an 'Entity', or in a racing game, a 'Car' - you need to be more specific to have it mean anything in your context. Interfaces provide you with a manner to say "here's an idea for a thing, but to actually get that thing, you have to flesh it out". Accelerating on different racecars is different, and so you can't just say 'Car' in Gran turismo, you have to select a type of car. Since there's no need to instantiate a car, you maybe create an interface, so everyone knows not to create an instance of that object.

In summary: readability and safety. Someone reading your code will immediately understand what it is, and somebody, even you, writing your code will never accidentally create an instance of that interface and expect it to work.

1

u/The_King1337 Intermediate Brewer Jun 20 '13

Thanks a bunch! You've explained it better and in more depth than anyone!

1

u/Zokkar Jun 20 '13

What about an abstract class?

1

u/[deleted] Jun 20 '13

Do you mean when you would use an abstract class, or are you asking what an abstract class is?

Basically, an abstract class is an interface, but that has some of its functions implemented. So it's kind of like, you guys all have to do these functions, but getX() will pretty much always return this.x, so why have each of you implement that? An abstract class will define common functions so that it's child classes can, if they want, inherit them, but they don't have to.

This is useful so you can follow DRY principles - Don't Repeat Yourself, which makes things less prone to error; it also gives a default implementation for things that make sense to have them, so that your new class can be up and running much quicker, and then later you can tweak some functions to be exactly how you like.

3

u/snot3353 COMPUTERS!!! Jun 19 '13

Probably the most significant difference is that a class can only extend one other class whereas it can implement any number of interfaces. On top of that, an interface is always 100% abstract. You cannot implement any of the methods to have a default concrete implementation whereas with a class or abstract class you can have either all the methods have implementations or some concrete and some abstract.

1

u/[deleted] Jun 19 '13 edited Jun 20 '13

Inheriting from a class limits your flexibility and says "This class is a subtype of this other class." Well, that may not be what you want. Implementing an interface just binds you to a 'contract' that your class is capable of acting a certain way (i.e. it implements certain methods). A class can implement any number of interfaces. To use a simplistic example, a Dog class can be Pettable, Walkable, Feedable, etc.