简体   繁体   中英

Missing annotation: @Override is not shown by compiler in Eclipse IDE

This is kind of weird! but when I implement Collection for my modal class and add unimplemented methods using Eclipse IDE, it is not showing @Override in any of the methods which are generated by clicking on "Add Unimplemented Methods".

public class MadeItACollection implements Collection{
}

When I click on "Add Implemented Methods" the following happens:

public class MadeItACollection implements Collection{

 public int size() {
    // TODO Auto-generated method stub
    return 0;
}

public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}
.
.
.
.
}

I dont see any @Override. I am compiling my codebase in Java 8. Am I missing something obvious?

As peoples' comments suggest, this is a Code Style option within Eclipse.

You can enable it under Preferences -> Java -> Code Style -- ensure that the "Add @Override annotation for new overriding methods" is checked; after, you can also look into adding it for implementations of interface methods via the link directly underneath. (You can also enable automatic adding of the annotation as a Cleanup or Save action in these menus.)

The @Override annotation isn't strictly required when implementing an interface mainly because you aren't overriding any superclass implementation you are actually implementing the interfaces declared methods. So maybe a @Implements annotation is required, but that's a whole different topic of conversation.

However, it is strongly recommended that you still use the @Override annotation on these methods because:

  1. It explicitly conveys to anybody reading the code that it is an overriding method.
  2. It helps avoid shooting yourself in the foot by throwing a compile time error if you misspell the method you want to override because it will tell you if the method can actually override an existing super method.

Most IDEs actually will help you add this automatically:

Intellij Idea:

Navigate to

File => Settings => Editor => Code Style => Java

And scroll to Override Method Signature and you should find Insert @Override annotation , make sure this is checked.

The Jetbrains documentation says the below about this setting option:

Insert @Override Annotation: Select this checkbox to have IntelliJ IDEA insert @Override annotations automatically.

缺少覆盖注释的 Itellij 设置

Eclipse:

Navigate to:

Window => Preferences => Java => Code Style

And look for Add @Override annotation for new overriding methods and make sure it has been checked.

用于为新覆盖方法添加覆盖注释的 Eclipse 设置

You can also add it as a Save Action by navigating to:

Window => Preferences => Java => Editor => Save Actions

And ensuring that Perform the selected actions on save has been selected and that Additional actions has been selected and ensuring that Add missing Annotations has been configured

缺少覆盖注释的 Eclipse 设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM