• Effective Java 36 Consistently use the Override annotation


    Principle

    Use the Override annotation on every method declaration that you believe to override a superclass declaration.

    // Can you spot the bug?

    public class Bigram {

    private final char first;

    private final char second;

    public Bigram(char first, char second) {

    this.first = first;

    this.second = second;

    }

    // This method is not the equals(Object) 's override, it's just a new method

    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()); // it will print 260 not 26.

    }

    }

    The @Override annotation will help you find the issue at the compile time.

    Bigram.java:10: method does not override or implement a method

    from a supertype

    @Override public boolean equals(Bigram b) {

    ^

    // The correct overriding

    @Override public boolean equals(Object o) {

    if (!(o instanceof Bigram))

    return false;

    Bigram b = (Bigram) o;

    return b.first == first && b.second == second;

    }

       

    Note

    It is worth annotating all methods that you believe to override superclass or super interface methods, whether concrete or abstract. For example, the Set interface adds no new methods to the Collection interface, so it should include Override annotations on all of its method declarations, to ensure that it does not accidentally add any new methods to the Collection interface.

       

    Summary

    The compiler can protect you from a great many errors if you use the Override annotation on every method declaration that you believe to override a supertype declaration, with one exception. In concrete classes, you need not annotate methods that you believe to override abstract method declarations(though it is not harmful to do so).

       

  • 相关阅读:
    [LeetCode] 39. Combination Sum 组合之和
    CSS3
    常见中文字体在CSS中的Unicode编码(宋体:5B8B4F53)
    List<Object> 使用Linq
    查看工作流详情页面
    java程序调用.net接口服务地址的写法
    C# Repeater 嵌套
    JavaScript刷新页面,不重复提交
    Migration-添加表(加外键)
    Migration-添加表
  • 原文地址:https://www.cnblogs.com/haokaibo/p/Consistently-use-the-Override-annotation.html
Copyright © 2020-2023  润新知