Java: Should I add an @Override annotation when implementing abstract methods?












45















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?










share|improve this question





























    45















    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?










    share|improve this question



























      45












      45








      45


      9






      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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
























          4 Answers
          4






          active

          oldest

          votes


















          64














          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.






          share|improve this answer



















          • 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



















          20














          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.






          share|improve this answer
























          • +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



















          13














          Yes. It is recommended practise by Joshua Bloch in Effective Java.






          share|improve this answer

































            6














            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.






            share|improve this answer
























            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            64














            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.






            share|improve this answer



















            • 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
















            64














            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.






            share|improve this answer



















            • 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














            64












            64








            64







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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














            • 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













            20














            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.






            share|improve this answer
























            • +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
















            20














            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.






            share|improve this answer
























            • +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














            20












            20








            20







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • +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











            13














            Yes. It is recommended practise by Joshua Bloch in Effective Java.






            share|improve this answer






























              13














              Yes. It is recommended practise by Joshua Bloch in Effective Java.






              share|improve this answer




























                13












                13








                13







                Yes. It is recommended practise by Joshua Bloch in Effective Java.






                share|improve this answer















                Yes. It is recommended practise by Joshua Bloch in Effective Java.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 10 '12 at 12:20

























                answered Jun 17 '09 at 8:56









                mR_fr0gmR_fr0g

                6,42122948




                6,42122948























                    6














                    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.






                    share|improve this answer
























                    • 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
















                    6














                    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.






                    share|improve this answer
























                    • 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














                    6












                    6








                    6







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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



















                    • 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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini