making interfaceOb

Discussion in 'Plugin Development' started by Pizza371, Dec 12, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Pizza371

    So who knows why interfaces are used? I was looking through a plugins source the other day, and they used interfaces quite a bit..
    I was wondering why interfaces are used? All I see is implementing an interface and creating a method that could of been done with exactly the same code..?
    In what way would an interface be productive by implementing it?
    I have no knowledge on interfaces at all, so simple explanations would be greatly appreciated (all I know is that they hold empty methods).
    I have googled around quite a bit, but they all seem to confuse me :p
    Thanks to anyone who answers!
     
  2. Offline

    The_Doctor_123

    Interfaces are great for when you want to have objects with a combination of properties. Interfaces are an alternative to multiple extensions since you may not extend more than one class in Java, but may implement as many interfaces as you'd like.

    But here's what I meant about "a combination of properties":
    So the interface, Player, inherits all these different interfaces. Quite a unique combination, don't you think? A minecart wouldn't implement things like CommandSender or LivingEntity, and it may implement other things to give it other properties a player wouldn't have.
     
    Pizza371 likes this.
  3. Offline

    Pizza371

    The_Doctor_123 thanks for clearing that up :)!
    I understand it a bit better now, but what I don't understand is what an interface HOLDS, or BRINGS to the class that is implementing it.
    example:
    Code:
    interface intorfoose {
    void emptyMethod();
    }
     
    ..
     
    class bestestclass implements intorfoose {
     
    @Override
    void emptyMethod() {
    }
     
    }
    that part is what I am most confused about (what's the point?).
     
  4. Offline

    The_Doctor_123

    Pizza371
    I understand what you mean, and it's hard for me to think of how to use it as well. You can most easily make your interface "contain stuff" with an inner class, though.
    Code:
    intorfoose inf = new intorfoose()
    {
    @Override
    void emptyMethod()
    {
    System.out.println("Hi there!");
    }
    };
    Like regular classes, you must implement all methods from the superinterface.
     
  5. Offline

    Wingzzz

    Pizza371
    I'll just go over briefly a few uses of it, I'd recommend going to external sources for more in-depth knowledge as the forums aren't necessarily the place for generic java questions, more so for specific help/advice with projects involving Bukkit.

    Anyways... Interfaces are used in polymorphism (Interface Animal -> class Cat implements Animal / class Dog implements Animal -- now both Cat and Dog can be referenced as Animal(s)). This can be shown with Bukkit's Listener interface. It contains no methods, nada. It is purely there to represent that a class is a 'Listener'. As well as Entity, there is a long line of subinterfaces that lead down to ones such as LivingEntity, HumanEntity etc etc... Player is a type of LivingEntity, although a snowball is not a type of LivingEntity, although it is an Entity, as well as a Projectile.

    It's used to define a contract with the user so that you're saying you can use: this, this, this and this... You're essentially giving them the methods in which the class contains without exposing the internals of how it works because that is irrelevant to them, they simply need to know what they can do with the object.

    Bukkit.getPlayer(String name); returns of type Player. Player is an interface with certain methods defined. We can only use those methods from a Player object. This is done purposefully to allow us safe access to the internals of Craftbukkit. Craftbukkit (the implementation) will return us an actual object that somewhere along the lines implements Player, then returns it back to us as a- Player. We don't care about CraftPlayer etc- the internals means nothing to us. We've been given 1/2 of the project which is Bukkit (the API) which will allow us to safely interact with the implementation. It doesn't matter if the way the implementation works changes, the API is what we're referencing.

    I can go more in-depth with inheritance/polymorphism with a better explaination and examples if you would like. Don't fret if you don't understand the importance of interfaces just yet, but if you continue to program with java you will surely hit a point where you will use them extensively.
     
    The_Doctor_123 likes this.
  6. Offline

    The_Doctor_123

    Wingzzz
    True, however, this isn't your typical "[URGEEENNT]howe dew i rite a if statment?!?"
     
    Wingzzz likes this.
Thread Status:
Not open for further replies.

Share This Page