zoukankan      html  css  js  c++  java
  • 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).

       

  • 相关阅读:
    oracle学习篇十:序列
    oracle学习篇九:同义词
    oracle相关常识
    oracle之数据同步:Oracle Sql Loader使用说明(大批量快速插入数据库记录)
    oracle学习篇八:约束
    oracle学习篇七:更新操作、事务处理
    oracle学习篇六:子查询
    oracle学习篇五:组函数,分组统计
    oracle学习篇四:多表查询
    oracle学习篇三:SQL查询
  • 原文地址:https://www.cnblogs.com/haokaibo/p/Consistently-use-the-Override-annotation.html
Copyright © 2011-2022 走看看