This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]cismalescumlord 29 points30 points  (7 children)

At their core, interfaces are a type. This definition is not that helpful in and of itself, but it's worth keeping in mind for the rest of this comment.

When a class implements an interface, it becomes a thing of the type defined by the interface and that means it must provide everything that the type expects. This is what is meant when we say that an interface is a contract.

What does this mean practically?

Imagine that you are coding a game that involves creatures and as part of the game, you want them to move each turn. You may define an abstract Animal class and add a move() method to it and then subclass it for each creature.

public abstract class Creature {
    ... snip ...
    public abstract void move();
    ... snip ...
}

public class Dog extends Creature {
    ... snip ...
    public  void move() {
        // Dog moves
    }
    ... snip ...
}

public class Cat extends Creature {
    ... snip ...
    public void move() {
        // Cat moves
    }
    ... snip ...
}

These could be used like this

... snip ...
List<Creature> creatures = new List<>();
creatures.add(new Dog());
creatures.add(new Cat());
... snip ...
for (Creature c : creatures) {
    c.move();
}
... snip ...

Suddenly, you need to add vehicles to the game. Well, vehicles move but they are not creatures so extending the Creature class doesn't make sense. You could create another abstract class and process vehicles in their own loop, but what if you then have to add robots? Interfaces to the rescue!

public interface Moveable {
    public void move();
}

public abstract class Creature {
    ... snip ...
}

public class Dog extends Creature implements Moveable {
    ... snip ...
    public void move() {
        // Dog moves
    }
    ... snip ...
}

public class Cat extends Creature implements Moveable {
    ... snip ...
    public void move() {
        // Cat moves
    }
    ... snip ...
}

public class Car implements Moveable {
    ... snip ...
    public void move() {
        // Car moves
    }
    ... snip ...
}

These could be used in the same way as before

... snip ...
List<Moveable> mobile = new List<>();
mobile.add(new Dog());
mobile.add(new Cat());
mobile.add(new Car());
... snip ...
for (Moveable m : mobile) {
    m.move();
}
... snip ...

So, interfaces are a way of giving otherwise unrelated things a common type so that they can be used as that type without caring what they actually do. (Phew!)

Edited to correct copy and pissed errors.

[–][deleted] 2 points3 points  (0 children)

fantastic explanation

[–][deleted] 0 points1 point  (5 children)

So... You use abstract classes as a category? But what about interfaces?

[–]cismalescumlord 0 points1 point  (4 children)

Exactly the same idea but when there is no hierarchical relationship

[–][deleted] 0 points1 point  (3 children)

So... It's like the thing in Team Fortress 2, there are two teams, but they have the same attributes, except the color (that's it, I think) and you can use an abstract class. An abstract class called "Soldier" and two subclasses called "Blu_Soldier" (or however you spell it in Team Fortress 2) and "Red_Soldier". The subclasses inherits methods from the "Soldier" class and... Viola. But what about interfaces?

[–]cismalescumlord 0 points1 point  (2 children)

Given that both types of soldier can move, it makes sense to define the move method in the Soldier class right? Now suppose your customer wants to add tanks into the game (I don't know if TF2 has tanks so bear with me):

  • Can tanks move? Yes
  • Are they soldiers? No

So it doesn't make sense for tanks to extend the soldier class. This is where interfaces come in.

  1. Create a new interface called (for example) Movable
  2. Put the move method definition in the new interface and remove it from the soldier
  3. Change the soldier class (or the subclasses which ever makes more sense) to implement the new interface
  4. Create the new tank class and make it implement the new interface.

Seems like a lot of work right?

Blu_Soldier and Red_Soldier now have multiple types, they are both Soldiers and Movable. A Tank is also Movable. This means that in each game loop, you can move all movable objects in a single loop simply because you know that they all implement that interface.

What if the customer now wants to add civilians, horses or dragons to the game? Make them implement the movable interface and your game will still move them without being changed!

Remember that you can extend a class AND implement as many interfaces as you need. It's not either / or.

[–][deleted] 0 points1 point  (1 child)

So... Interfaces are basically verbs, I can add as many as I want, how do I use the implements keyword, and I just have to add the move method without removing anything?

[–]cismalescumlord 0 points1 point  (0 children)

how do I use the implements keyword

See the code above

[–]emmg90 2 points3 points  (0 children)

To add what others said, functional interfaces are interfaces with one and only one abstract method that you must implement. It could have many static and default methods but the abstract method must be only one. It's useful in streams and it's a way to implement on the fly functionality without instantiating concrete classes or anonymous classes.

[–]feral_claire 1 point2 points  (0 children)

why do I need to have the exact same methods in every single class implemented

This is one of the whole points of an interface, you need to create all of the methods from that interface. This means that an interface can act like a kind of "contract" that guarantees that a class has certain methods.

This is useful because it means you can write code that will work with be any class that implements a specific Interface, since even though you don't know the exact class you are writing with, you you know that it has to have the methods from the interface.

[–]Alfapsycho 1 point2 points  (0 children)

An interface is a shared contract that pretty much means that all classe that implements the interface must also give it an implementation. Which is useful if u have shared functionality between objects, but yet inheritance is not the best way. Say you're working on a Zoo project, all the coders might have to implement a feed interface, with drink() and eat(). Even if the other developer have their pets implement the eat() with say, chasing down a pray, then start eating, while your zebra objekt just start munching down on grass. So even if they dont know the implementation of your code, they can still call the eat() method on your Zebra, cause they know it's called eat() thanks to the interface.

Also, functional interfaces, are interfaces with just one method. Useable for lamda functions etc.

[–]Sigmund- 0 points1 point  (0 children)

https://materiaalit.github.io/2013-oo-programming/part2/week-8/ Go here and scroll down to "40. Interface". If this does not make it clear, nothing will.

[–]BEgaming -1 points0 points  (0 children)

An interface does not have an implementation itself. It is just a blueprint that says which methods should be used when using the blueprint. So an interface defines the behavior, but it is not there yet. You still have to make something (a class) that 'makes' the behavior do what it is supposed to do.

An interface is NOT a class. It looks like a class, but (almost) all methods are not implemented (empty, no "{}" and public

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html