0

I am using the Bukkit API to create Minecraft plugins. The Player interface can be called and used like so:

Player p = [insert player here];
String name = p.getName();

However, as Player is an interface, which only has the method and not the method body, how can an interface know what method to do when you do the p.getName() method? The interface has that method in it, but defines no code to execute? As I understand it, you have to define your interface in classes, but since you can have multiple classes implementing the same thing, how does making a call to the interface like that return the name? What am I missing?

Thank you for your time.

Connor M
  • 331
  • 3
  • 5
  • 18
  • That is the magic of interface, you don't mind about implementation details only centered in `what` they do rather than `how` – nachokk Jan 14 '14 at 01:23
  • Yes, but I would like to know how they work in case I need to use one in my code. – Connor M Jan 14 '14 at 01:29
  • @ConnorM Have a look at "programming with interfaces" – Dennis Meng Jan 14 '14 at 01:30
  • Well, in somewhere you have to inject what actually object reference `p` via dependency injection, constructor injection, reflection or instantation in code – nachokk Jan 14 '14 at 01:31

4 Answers4

4

Even though Player itself is an interface, whatever p refers to after that first line is an instance of a class that implements the Player interface. It is that class that determines which implementation of getName() is called.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
  • Isn't it possible to implement Player in more than one class, though, and therefore have different code to run in each class? Would it run both methods, one from each class, if this happened? – Connor M Jan 14 '14 at 01:27
  • @ConnorM Yes, multiple classes can implement the `Player` interface. `p` however, will always refer to an instance of a *specific* class that implements it. – Dennis Meng Jan 14 '14 at 01:29
  • If it refers to a specific class that implements it, how is it decided which one to use? Would I have to specify which one if there was more than one class that implemented it? – Connor M Jan 14 '14 at 01:31
  • 1
    @ConnorM The idea is that in order for that line to execute, the `[insert player here]` part must return an object that is an instance of a class implementing `Player`. It is *that* class that will "specify" it for you. – Dennis Meng Jan 14 '14 at 01:33
  • 1
    Oh, I guess that's the part I didn't understand about what Bukkit was doing. I thought it was just returning an instance of Player, didn't realize it was returning an implementation of it. Thanks – Connor M Jan 14 '14 at 01:35
  • @ConnorM Well, for it to be able to return an instance, it has to be backed by some class that implements the interface. – Dennis Meng Jan 14 '14 at 01:39
  • 1
    @ConnorM `I thought it was just returning an instance of Player` that is right, it's returning an instance of Player. An instance of Player is an instance of a implementation of Player interface. You can't instantiate an interface! interface is just the contract – nachokk Jan 14 '14 at 01:39
  • @ConnorM Note that if you say `new Player(...)`, the Java compiler will complain. You can only use `new` on a class, not an interface (and it can't be an abstract class, but that's a separate topic). There's no such thing as an object that is merely "an instance of `Player`", without also being an instance of some concrete class. – ajb Jan 14 '14 at 01:46
  • @ajb well strictly speaking you can make it as an anonymous class. – nachokk Jan 14 '14 at 01:49
  • @nachokk Right, `new Player() { body }` is legal. – ajb Jan 14 '14 at 02:08
  • For those interested, Bukkit's implementation of Player is taken care of over in the CraftBukkit side in form of `CraftPlayer`: https://github.com/Bukkit/CraftBukkit/blob/master/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java – Slate Jan 23 '14 at 13:34
2

The variable p is associated with the Player interface at compile time, but the thing being referenced by p -- an instance of some concrete class that implements Player -- is bound at runtime. It's this object that contains the method's body.

Notice that p can refer to an instance of any class that implements the Player interface. That is, at runtime, you will create some object [insert player here] and it's that object's version of getName -- whatever that is -- that will be executed. (Furthermore, in Java, you must instantiate some implementation of Player; you cannot (logically or legally) create a Player object directly.)

This so-called late binding doesn't happen until the program is executed and the Player instance is created. This is a fundamental aspect of polymorphism in object-oriented programming.

Wayne
  • 59,728
  • 15
  • 131
  • 126
1

Being an interface, Player is an abstract type (in computer science terms).

A concrete type (a class, which can be instantiated) that implements Player will be used for the instance. For example:

public interface Player {
    public String getName();
}

public class StandardPlayer implements Player {

    private String name;

    public String getName() {
        return name;
    }
}

Then

Player p = new StandardPlayer();
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

The Player interface is from the Bukkit API.

The body of the Player interface is defined in the CraftPlayer class which is from the server software Craftbukkit.

Azoraqua
  • 155
  • 1
  • 12