When do I need to use @Override

Discussion in 'Plugin Development' started by Itay080, Jan 14, 2016.

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

    Itay080

    Hi,
    I have recently noticed that some developers use @Override below their onEnable and onDisable method, what is it doing? My plugins are working perfectly without, do I even need it?
     
  2. Offline

    ski23

    @Override overrides an inherited method from a parent. I would suggest you learn some more common java on inheritance.
     
  3. Offline

    mcdorli

    This is a java question. Override does what it says, it overrides the method from the extended class, and you don't create a new one, it doesn't actually matter in terms of runtime.
     
  4. Offline

    teej107

    You don't need @Override. You can override methods without using that annotation. It's just good practice to use that annotation because it's easy to see that you are overriding a method and the compiler will complain if a method with the @Override annotation can't be overridden.
     
  5. Offline

    mcdorli

    And helps to avoid the issues, where someone complains about how the onCommand method has a bug, when he misstyped it to onComand
     
  6. Offline

    teej107

    Yes. That is a good example. Also some people write the onEnable/Disable methods with a capital O.

    @Itay080 If you don't understand when a method is overridden, then you need to learn about inheritance. It's basic OOP stuff anyway that you should already know.
     
  7. Offline

    Itay080

    Seems like this is the only thing I skipped when studying basic Java..
    Thanks.
     
  8. Offline

    mythbusterma

    @Itay080

    It's not really tutorialised in many places, so it's pretty easy to miss.
     
    Itay080 likes this.
  9. Offline

    ChipDev

    Doesn't it make it so I don't need to do
    onEnable
    and can do
    onEnAbLe? (I wouldn't do that, just wondering...)
     
  10. Offline

    ski23

    @ChipDev Not sure. Interesting question.
     
  11. Offline

    teej107

    No
     
  12. Offline

    mythbusterma

    @ChipDev

    It ensures that a method of that exact signature exists in some parent class or interface, and avoids common typography errors like the ones you showed. It is an annotation that is only used by the compiler and is erased when the class is compiled.
     
    ChipDev likes this.
  13. Offline

    mcdorli

    Btw, does annotations, like eventhandler stay there or if doesn't, then how does bukkit find them?
     
  14. Offline

    mythbusterma

    @mcdorli

    EventHandler is kept after compiling.
     
  15. Offline

    mcdorli

    Cheers
     
  16. Offline

    Itay080

    Uhm, I have a pretty stupid question, when I use @Override, or @EventHandler, it only stays for the void after it, right?
    For example, if I have:
    void hi() {

    }

    @EventHandler
    void hello() {

    }
    void wassup(){

    }

    Only hello is going to be an event, right?
     
  17. Offline

    mcdorli

    Nope, it would show an error, because hello doesn't have any arhuments
     
  18. Offline

    ChipDev

    Yeah, its good practice to use ;)

    Yes- like @mcdorli said, it will throw an error, but only hello will be an EventHandler

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  19. Offline

    Itay080

    Yea I know that it will throw an error, that was just for the example.
     
Thread Status:
Not open for further replies.

Share This Page