Java: Should I add an @Override annotation when implementing abstract methods?
When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?
java annotations override
add a comment |
When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?
java annotations override
add a comment |
When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?
java annotations override
When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?
java annotations override
java annotations override
edited Nov 11 '15 at 9:52
Ellen Spertus
2,99673667
2,99673667
asked Jun 17 '09 at 8:52
EyvindEyvind
2,71843252
2,71843252
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
I tend to prefer the use of @Override
in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).
The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.
4
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
add a comment |
Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"
Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
1
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
add a comment |
Yes. It is recommended practise by Joshua Bloch in Effective Java.
add a comment |
Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override
annotation because the compiler would give an error anyway. However, "it is not harmful to do so".
I'd recommend choosing a strategy and sticking with it consistently.
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
1
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
1
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
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%2f1005898%2fjava-should-i-add-an-override-annotation-when-implementing-abstract-methods%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
I tend to prefer the use of @Override
in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).
The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.
4
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
add a comment |
I tend to prefer the use of @Override
in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).
The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.
4
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
add a comment |
I tend to prefer the use of @Override
in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).
The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.
I tend to prefer the use of @Override
in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).
The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.
answered Jun 17 '09 at 8:56
Andrzej DoyleAndrzej Doyle
84.9k27174217
84.9k27174217
4
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
add a comment |
4
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
4
4
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
Also you need Java 6 to leverage this annotation. Java 5 does not allow you to place it on interface implementations.
– akarnokd
Jun 17 '09 at 9:03
add a comment |
Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"
Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
1
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
add a comment |
Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"
Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
1
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
add a comment |
Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"
Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.
Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"
Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.
answered Jun 17 '09 at 8:56
Jon SkeetJon Skeet
1088k68979408439
1088k68979408439
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
1
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
add a comment |
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
1
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
+1 for the "regression test"
– guerda
Jun 17 '09 at 8:58
1
1
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
That saved my skin a few times.
– Ravi Wallau
Jun 17 '09 at 15:37
add a comment |
Yes. It is recommended practise by Joshua Bloch in Effective Java.
add a comment |
Yes. It is recommended practise by Joshua Bloch in Effective Java.
add a comment |
Yes. It is recommended practise by Joshua Bloch in Effective Java.
Yes. It is recommended practise by Joshua Bloch in Effective Java.
edited Apr 10 '12 at 12:20
answered Jun 17 '09 at 8:56
mR_fr0gmR_fr0g
6,42122948
6,42122948
add a comment |
add a comment |
Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override
annotation because the compiler would give an error anyway. However, "it is not harmful to do so".
I'd recommend choosing a strategy and sticking with it consistently.
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
1
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
1
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
add a comment |
Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override
annotation because the compiler would give an error anyway. However, "it is not harmful to do so".
I'd recommend choosing a strategy and sticking with it consistently.
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
1
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
1
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
add a comment |
Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override
annotation because the compiler would give an error anyway. However, "it is not harmful to do so".
I'd recommend choosing a strategy and sticking with it consistently.
Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override
annotation because the compiler would give an error anyway. However, "it is not harmful to do so".
I'd recommend choosing a strategy and sticking with it consistently.
answered Jan 5 '10 at 10:32
MarkMark
1,15611726
1,15611726
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
1
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
1
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
add a comment |
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
1
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
1
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
The compiler would give an error anyway? You mean if a concrete class implements a method that is not an abstract method of a superclass, the compiler would complain about it? That can't be what you mean, but I can't figure out what you do mean.
– LarsH
Aug 12 '15 at 1:41
1
1
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
@LarsH He means that the compiler will give an error if the concrete class fails to implement an abstract method in the superclass, which would be the case if you misspelled the name of the abstract method that the subclass needed to implement.
– Ellen Spertus
Nov 11 '15 at 9:51
1
1
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@espertus: Thanks for the clarification. So you would not get an error or warning if, as Andrzej said, the method in the superclass/interface is changed or removed.
– LarsH
Nov 11 '15 at 16:49
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
@LarsH Right, you would only get the error/warning if you used the Override tag.
– Ellen Spertus
Nov 13 '15 at 17:15
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%2f1005898%2fjava-should-i-add-an-override-annotation-when-implementing-abstract-methods%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