[JAVA] Why you should add the Override annotation when overriding a method

Override annotation

Annotation added in Java 1.5. It can be used only for method declaration to indicate that it is overriding a method of the parent class. Override is possible without the Override annotation. So why should you add the Override annotation? That is this article.

Why you should add the Override annotation

Suppose you have a parent class like this:

Parent.java


public class Parent {

    private String something;

    protected void setSomething(String something) {
        this.something = something;
    }
}

I wrote the following code because I needed to inherit from this class and override the setSomething method.

Child.java


public class Child extends Parent {

    protected void setSomething(Integer something) {
        System.out.println(something);
    }
}

I made a mistake in the argument type, but I can't notice the mistake because it compiles normally. I don't think it's true that the release will be released without noticing it as it is, but there is a possibility that you will definitely get hooked on the test as an unexpected bug. And here is the reason for adding the Override annotation. If you add the Override annotation, the IDE will detect it as shown below, so you can notice the mistake and solve the problem in advance (of course, the compiler will also detect compilation with the javac command). キャプチャ.PNG

Also, if you are using the IDE, it is possible to issue a warning if there is a method that overrides the method of the parent class even if the Override annotation is not attached depending on the setting. In other words, you can always use the Override annotation to detect unintended overrides.

Summary of reasons for adding Override annotation

  1. You can prevent inadvertent mistakes such as not being able to override it in advance, and prevent unexpected bugs.
  2. I didn't mean to override it, but when I looked closely I realized that I was overriding it, which helps prevent unexpected bugs.

that's all.

reference

Recommended Posts

Why you should add the Override annotation when overriding a method
How to reference a column when overriding the column name method in ActiveRecord
When you want to use the method outside
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
When you want to add a string type column with a limited length with the `rails generate migration` command
When will you do the refactoring?
Item 40: Consistently use the Override annotation
Do you need a memory-aware implementation of Java?
A story that failed using "bundle exec rubocop -a"
Why you need an email address for user registration
Do you need dotenv-rails?
Why you need setters / getters in the first place
[IOS] What you need to know before creating a widget
Why you need attr_accessor and why you don't (Thanks to Rails): Rails Tutorial Memorandum--Chapter 9
Why you should add the Override annotation when overriding a method
A memo when you want to clear the time part of the calendar