How @Override annotation in java can check misspelling of a method?2019 Community Moderator ElectionHow do I efficiently iterate over each entry in a Java Map?How do I call one constructor from another in Java?Fastest way to determine if an integer's square root is an integerHow do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?'Must Override a Superclass Method' Errors after importing a project into EclipseWhich @NotNull Java annotation should I use?How do I convert a String to an int in Java?
weren't playing vs didn't play
Do I really need to have a scientific explanation for my premise?
Why the color red for the Republican Party
Is it possible to avoid unpacking when merging Association?
Do f-stop and exposure time perfectly cancel?
Single word request: Harming the benefactor
Does the nature of the Apocalypse in The Umbrella Academy change from the first to the last episode?
Doesn't allowing a user mode program to access kernel space memory and execute the IN and OUT instructions defeat the purpose of having CPU modes?
Difference on montgomery curve equation between EFD and RFC7748
How is the wildcard * interpreted as a command?
Should I tell my boss the work he did was worthless
Motivation for Zeta Function of an Algebraic Variety
What does "the touch of the purple" mean?
How many characters using PHB rules does it take to be able to have access to any PHB spell at the start of an adventuring day?
List elements digit difference sort
What Happens when Passenger Refuses to Fly Boeing 737 Max?
Counting all the hearts
Can I pump my MTB tire to max (55 psi / 380 kPa) without the tube inside bursting?
Filtering SOQL results with optional conditionals
What problems would a superhuman have whose skin is constantly hot?
Word for a person who has no opinion about whether god exists
Makefile strange variable substitution
They call me Inspector Morse
How did Alan Turing break the enigma code using the hint given by the lady in the bar?
How @Override annotation in java can check misspelling of a method?
2019 Community Moderator ElectionHow do I efficiently iterate over each entry in a Java Map?How do I call one constructor from another in Java?Fastest way to determine if an integer's square root is an integerHow do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?'Must Override a Superclass Method' Errors after importing a project into EclipseWhich @NotNull Java annotation should I use?How do I convert a String to an int in Java?
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)
java annotations
add a comment |
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)
java annotations
add a comment |
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)
java annotations
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)
java annotations
java annotations
edited Mar 7 at 7:05
Tomka Koliada
1,0501628
1,0501628
asked Mar 7 at 6:33


Jin LeeJin Lee
183112
183112
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.".
add a comment |
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.
what block do you mean?
– Stultuske
Mar 7 at 6:46
1
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
1
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
add a comment |
The answer is simple.
@Override annotation indicates the compiler that the method below the override annotation is overridden i.e., 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 (i.e., 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 i.e, type and order of arguments along with method name.
add a comment |
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.
1
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037449%2fhow-override-annotation-in-java-can-check-misspelling-of-a-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.".
add a comment |
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.".
add a comment |
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.".
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.".
answered Mar 7 at 6:39


SweeperSweeper
69.8k1074142
69.8k1074142
add a comment |
add a comment |
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.
what block do you mean?
– Stultuske
Mar 7 at 6:46
1
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
1
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
add a comment |
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.
what block do you mean?
– Stultuske
Mar 7 at 6:46
1
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
1
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
add a comment |
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.
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.
edited Mar 7 at 6:45
answered Mar 7 at 6:41
StultuskeStultuske
6,41511729
6,41511729
what block do you mean?
– Stultuske
Mar 7 at 6:46
1
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
1
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
add a comment |
what block do you mean?
– Stultuske
Mar 7 at 6:46
1
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
1
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
what block do you mean?
– Stultuske
Mar 7 at 6:46
what block do you mean?
– Stultuske
Mar 7 at 6:46
1
1
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
I was editing that myself the same time you were :)
– Stultuske
Mar 7 at 6:49
1
1
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
A small note, you may want to replace Exception with (Compiler)Error, as the compiler doesn't throw an exception, but errors
– Lino
Mar 7 at 6:53
add a comment |
The answer is simple.
@Override annotation indicates the compiler that the method below the override annotation is overridden i.e., 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 (i.e., 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 i.e, type and order of arguments along with method name.
add a comment |
The answer is simple.
@Override annotation indicates the compiler that the method below the override annotation is overridden i.e., 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 (i.e., 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 i.e, type and order of arguments along with method name.
add a comment |
The answer is simple.
@Override annotation indicates the compiler that the method below the override annotation is overridden i.e., 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 (i.e., 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 i.e, type and order of arguments along with method name.
The answer is simple.
@Override annotation indicates the compiler that the method below the override annotation is overridden i.e., 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 (i.e., 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 i.e, type and order of arguments along with method name.
answered Mar 7 at 6:57


Anirudh KabdeAnirudh Kabde
363
363
add a comment |
add a comment |
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.
1
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
add a comment |
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.
1
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
add a comment |
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.
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.
answered Mar 7 at 6:47
Koustubh MadkaikarKoustubh Madkaikar
425
425
1
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
add a comment |
1
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
1
1
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
"will result in a warning", actually, it will result in an Exception when you try to compile the code.
– Stultuske
Mar 7 at 6:48
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
@Koustubh Madkaikar thank you for your answer~~~~~
– Jin Lee
Mar 7 at 6:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037449%2fhow-override-annotation-in-java-can-check-misspelling-of-a-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown