简体   繁体   中英

@override annotation

Do I need to put @Override annotation when 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基本上强制编译器检查你是否真的覆盖了现有的基类方法,而不是简单地(意外地)创建新方法。

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() ; ie, 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)

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