简体   繁体   中英

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

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)

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.".

Your understanding is wrong.

First: it's @Override . Java is case sensitive, so yes, the distinction is important. Secondly:

I've learned that it overrides a parent method.

No. It just marks the fact that the method it is applied to does. Just because you slap @Overrides on a method, doesn't magically make it override something. It'll cause Exceptions if it doesn't.

As for checking for misspelling: let's say you want to override the toString() method.

@Override
public String toString() {
  return "myString";
}

will be successful, because toString does exist like that in a parent class (Object).

However, when you try:

@Override
public String toSttring() {
  return "one t too many";
}

it will search in parent classes and implemented interfaces. If it can't find this method signature anywhere, it will throw an Exception. Seeing as this most likely should have been toString() instead of toSttring() , that's one check.

It doesn't, however, just check for spelling. It basically checks the method signature (which includes the method name).

If you were to try:

@Override
public Double toString() {
  return new Double("5");
}

Unless you have added this method yourself in a parentclass of your class, this will fail, since the Objects toString returns a String.

Just like:

@Override
public String toString(String value) {
  return value;
}

is likely to fail, for the same reasons.

EDIT: Just for clarification: @Override doesn't do a spellcheck. If you misspelled the method name in the parent class, it won't work if you spell it correctly in the child class and add the Override annotation.

The answer is simple.

@Override annotation indicates the compiler that the method below the override annotation is overridden ie, a method with matching signature is expected to be present in one of the parent classes in the hierarchy of the class in which the method is being written at the time of writing the code.

It checks at compiler time, in the hierarchy (ie, parent class, grand parent class, etc) whether the current method you have written has a method with the same signature. If not, it raises the error. It's not about spell check. It's about signature match ie, type and order of arguments along with method name.

java @Override annotation will make sure any super-class changes in method signature will result in a warning and you will have to do necessary changes to make sure the classes work as expected.

It's better to resolve potential issues at compile time than run-time. So always use java @Override annotation whenever you are trying to override a super-class method.

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