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).

Effective Java 36 Consistently use the Override annotation的更多相关文章

  1. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  2. 《Effective Java》读书笔记 - 6.枚举和注解

    Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. Effective Java 第三版——40. 始终使用Override注解

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. Effective Java 第三版——36. 使用EnumSet替代位属性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. java 重载父类报错 Remove '@override' annotation解决办法

    Remove '@override' annotation解决办法      最近刚刚配置了新机器,将原来的代码放在eclipse上执行,总会出现Remove '@override' annotati ...

  8. Effective Java 08 Obey the general contract when overriding equals

    When it's the case that each instance of the class is equal to only itself. 1. Each instance of the ...

  9. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

随机推荐

  1. [git]安装git-pylint-commit-hook提高python项目中的代码质量

    什么是'git-pylint-commit-hook' 我在工作中,团队为了保证代码和提高代码的质量,要求每个项目都要求安装git-pylint-commit-hook,它是个钩子,会在你提交代码到本 ...

  2. linux内核更新前后配置文件的比较

    说明:这里先给出一个比较的结果,作为记录,后续会给出内核配置差异的详细解释. [root@xiaolyu linux-4.7.2]# diff .config .config_bak  3c3< ...

  3. Scrum 项目 7.0 Sprint回顾

    7.0------------------------------------------------ Sprint回顾 让我们一次比一次做得更好.   1.回顾组织 主题:“我们怎样才能在下个spr ...

  4. MVC知识点02

    MVC基础知识详情 1:在MVC中如果要从前台页面(.aspx)获取参数,只需要将其两个页面的参数设置成一样的,这样子MVC中的机制就会自动的将参数的值传到方法中. 2:在MVC中的方法要是两个都是相 ...

  5. [爬虫学习笔记]用于提取网页中所有链接的 Extractor 模块

            Extractor的工作是从下载的网页中将它包含的所有URL提取出来.这是个细致的工作,你需要考虑到所有可能的url的样式,比如网页中常常会包含相对路径的url,提取的时候需要将它转换 ...

  6. 可访问性级别的C# 修饰符

    使用访问修饰符 public.protected.internal 或 private 可以为成员指定以下声明的访问级别之一. http://keleyi.com/a/bjad/3ccfqh95.ht ...

  7. SQL Server数据库ROW_NUMBER()函数使用详解

    SQL Server数据库ROW_NUMBER()函数使用详解 摘自:http://database.51cto.com/art/201108/283399.htm SQL Server数据库ROW_ ...

  8. Python记录-Pip安装

    1.第一步 下载py文件:https://bootstrap.pypa.io/ez_setup.py #!/usr/bin/env python """ Setuptoo ...

  9. PostgreSQL avg()函数

    PostgreSQL的AVG函数是用来找出各种记录中的一个字段的平均值. 为了理解AVG函数考虑表COMPANY 有如下记录: testdb# select * from COMPANY; id | ...

  10. css中inline、block、inline-block的区别

    http://www.cnblogs.com/fxair/archive/2012/07/05/2577280.html display:inline就是将元素显示为块级元素. block元素的特点是 ...