How @Override annotation in java can check misspelling of a method?

Jin Lee :

I was looking through information on @Override annotation in Stackoverflow .

I've learned that it overrides a parent method. However,I saw some comments saying that @Override can in fact check misspelling, and it is quite useful to put @override annotation before all methods.

I want to know how @Override can check misspelling of a method name. (if I understood correctly)

Sweeper :

Let's I want to override this method:

public class Foo {
    public void foo() { ... }
}

Without @Override, I can still override it:

public class Bar extends Foo {
    public void foo() { ... }
}

But if I misspelled it:

public class Bar extends Foo {
    public void fooo() { ... }
}

fooo does not override foo. If I didn't realise the misspelling, I wouldn't know that foo is actually not overridden. This can cause bugs.

If I add @Override to fooo, however:

public class Bar extends Foo {
    @Override
    public void fooo() { ... }
}

The compiler will tell me that fooo does not override anything. I will see this message and go "But it does override foo! Why did the compiler say that it doesn't? Oh wait! I misspelled it.".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=112419&siteId=1