开发者

@override annotation

Do I need to put @Override annotation whe开发者_开发百科n I implement an interface (not override an abstract class)?

And what does @Override annotation achieve?


In Java 5, you must not add @Override when implementing a method inherited from an interface, in Java 6, you should (or you'll get a compiler warning).

@Override asserts that a method is intended to override something, and will cause the compiler to notify you should this not or no longer be the case, for instance because the method you are overriding has been renamed.


It breaks your compile if you say you override something when you really didn't.

If you don't put an @Override tag, but according to the compiler you didn't override anything, you have a silent bug you don't know about. With the @Override tag, you do know about it, and you know about it NOW, not later.

You NEVER NEED to put an @Override annotation. But I'd recommend doing it every time.


The @Override annotation informs the compiler that the element is meant to override an element declared in a superclass.

It is not required, but it will generate a compile error if that method actually does not correctly override a method in a superclass.


@override basically forces the compiler to check that you really are overriding an existing base-class method, and not simply (accidentally) creating a new method.


It's good practice to always use @Override, as it can help you preemptively catch some otherwise insidious bugs. It also enhances code clarity. For example, check out the example given in Effective Java, Chapter 6, Item #36:

// Can you spot the bug? - Page 176
package org.effectivejava.examples.chapter06.item36;

import java.util.HashSet;
import java.util.Set;

public class Bigram {
    private final char first;
    private final char second;

    public Bigram(char first, char second) {
        this.first = first;
        this.second = second;
    }

    public boolean equals(Bigram b) {
        return b.first == first && b.second == second;
    }

    public int hashCode() {
        return 31 * first + second;
    }

    public static void main(String[] args) {
        Set<Bigram> s = new HashSet<Bigram>();
        for (int i = 0; i < 10; i++)
            for (char ch = 'a'; ch <= 'z'; ch++)
                s.add(new Bigram(ch, ch));
        System.out.println(s.size());
    }
}

This method may look fine at first glance, but it is in fact very broken! The programmer didn't follow the contract of equals(Object o), instead implementing equals(Bigram b), which will not be called when doing things that depend on equals(); i.e., inserting instances of Bigram into a Map. So, this will cause confusing, subtle wrong behavior that will be hard to track down, whereas if the programmer had just used @Override in the first place, the compiler would have immediately alerted them to the problem.


NO, Java doesn't bind you to put an @Override annotation while implementing an interface. You can override the methods declared in interface as we normally do. But, its a good practice to use an @Override annotation while overriding methods of an interface or any parent class including abstract class. Why? Because it helps us/compiler identify any problem in overriding a method at compilation itself.

For example: http://java-dive.blogspot.in/

 class Ball{
        public void bounce(){
        }
        } 

//////////////////////Override without @Override/////////////////////////////

    public class FootBall{
    public void bounce(String s){
    }
    }

//this will compile however new FootBall().bounce(); will call parent method.

//////////////////////Override with @Override/////////////////////////////

    public class FootBall{
    @Override
    public void bounce(String s){
    }
    }

//This will not compile as the parent class doent have any method with signature bounce(String s)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜