Principles

  1. The class must document its self-use of overridable methods.
  2. A class may have to provide hooks into its internal workings in the form of judiciously chosen protected methods.
  3. The only way to test a class designed for inheritance is to write subclasses. You must test your class by writing subclasses before you release it.
  4. Constructors must not invoke overridable methods. This happens when there is a method which can be override by subclass calls the subclass's constructor within it. Sample violates this rule:

    public class Super {

    // Broken - constructor invokes an overridable method

    public Super() {

    overrideMe();

    }

    public void overrideMe() {

    }

    }

    public final class Sub extends Super {

    private final Date date; // Blank final, set by constructor

    Sub() {

    date = new Date();

    }

    // Overriding method invoked by superclass constructor

    @Override public void overrideMe() {

    // This will fail in the constructor of the Super class.

    System.out.println(date);

    }

    public static void main(String[] args) {

    Sub sub = new Sub();

    sub.overrideMe();

    }

    }

  5. The Cloneable and Serializable interfaces present special difficulties when designing for inheritance. So it's not good idea for a class designed for inheritance to implement either of these inheritance. neither clone nor readObject may invoke an overridable method, directly or indirectly. In the case of the readObject method, the overriding method will run before the subclass's state has been deserialized. In the case of the clone method, the overriding method will run before the subclass's clone method has a chance to fix the clone's state.
  6. If you decide to implement Serializable in a class designed for inheritance and the class has a readResolve or writeReplace method, you must make the readResolve or writeReplace method protected rather than private. If these methods are private, they will be silently ignored by subclasses.

Summary

  1. Designing a class for inheritance places substantial limitations on the class.
  2. The best solution to this problem is to prohibit subclassing in classes that are not designed and documented to be safely subclassed. Two ways to accomplish this:
    1. Declare the class final.
    2. Make all the constructors private or package-private.
  3. Separate ordinary API documentation from information of interest only to programmers implementing subclasses.

Effective Java 17 Design and document for inheritance or else prohibit it的更多相关文章

  1. Effective Java 40 Design method signatures carefully

    Principle Choose method names carefully. Don't go overboard in providing convenience methods. Avoid ...

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

  3. 《Effective Java》读书笔记 - 4.类和接口

    Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计 ...

  4. Effective Java 目录

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

  5. 【Effective Java】阅读

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

  6. Effective Java 第三版——17. 最小化可变性

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

  7. Effective java笔记(二),所有对象的通用方法

    Object类的所有非final方法(equals.hashCode.toString.clone.finalize)都要遵守通用约定(general contract),否则其它依赖于这些约定的类( ...

  8. Effective Java通俗理解(持续更新)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

  9. Effective Java通俗理解(下)

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

随机推荐

  1. SQL Server里因丢失索引造成的死锁

    在今天的文章里我想演示下SQL Server里在表上丢失索引如何引起死锁(deadlock)的.为了准备测试场景,下列代码会创建2个表,然后2个表都插入4条记录. -- Create a table ...

  2. AutoMapper配置方法

    在Mvc开发中,我们经常需要构建一个viewModel出来供页面使用,在PO和VO之间相互传值的时候,如果实体字段比较多的时候,那么传值将变得异常麻烦,也使得代码非常的臃肿.AutoMapper可以帮 ...

  3. Linq之select子句

    在Linq中select子句用来指定查询结果的类型和表现形式.Linq查询要么以select子句结尾,要么以group子句结尾. List<UserBaseInfo> users = ne ...

  4. 自定义ConfigurationSection,创建多个嵌套的ConfigurationElementCollection节点

    由于接口地址都是固定的,所以想到使用自定义节点,来将接口都配置到web.config中. 很快,v1.0版本出炉: public class RequestConfigSection : Config ...

  5. petapoco-SQLServer模型增加注释

    petapoco是个基于T4模板的轻量级ORM,好用效率高,具体介绍略了 获取注释基本原理是调用数据库::fn_listextendedproperty函数,获取扩展属性MS_Description ...

  6. Servelet面试题

    1. Servlet与JSP有什么区别? Servlet和JSP完成的功能是相同的,都可以接收用户的请求,可以对用户进行响应,可以调用业务方法. 不同点在于JSP是在html或者xml中嵌入了Java ...

  7. 【C#进阶系列】02 PE文件,程序集,托管模块,元数据——还是那个Hello world

    好了,还是这张图,还是一样的Hello world. 因为本章其实很多都是讲一些命令行编译啊什么鬼的配置类的东西,要用的时候直接百度或者回头查书就可以了, 所以了解一下也就行了,也没有记录下来,接下来 ...

  8. Python调用C的SDK出现返回值不符合预期以及Segmentation fault

    1.sdk返回值不是int型 1.1 登录函数调用 def login(ip, port, username, password, device_info, error_code):"&qu ...

  9. UVA 10090 - Marbles 拓展欧几里得

    I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...

  10. Hibernate的缓存技术详解

    转载注明出处:http://www.cnblogs.com/xiaoming0601/p/5882980.html 一.什么是缓存: 并不是指计算机的内存或者CPU的一二级缓存:缓存是指为了降低应用程 ...