Connor M Connor M - 5 months ago 7x
Java Question

Where does an Interface get its method body from in Java?

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.


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.