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.

最新文章

  1. cs端调用Ajax
  2. JQuery知识点链接
  3. 使用python实现栈和队列
  4. HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)
  5. Java基础(62):Eclipse调试(Debug)的10条技巧(转)
  6. PHP JSON 操作总结
  7. 【转】安装Ubuntu 15.10后要做的事
  8. 为 ASP.NET Web API 创建帮助页
  9. C调用OPENSSL做REST服务客户端的例子
  10. 关于封装unity3d的dll时候的进一步总结
  11. Windows XP密钥(共38枚)
  12. NSDate常用代码范例
  13. mysql技术内幕InnoDB存储引擎-阅读笔记
  14. 找到一个新的超好用的U盘启动制作工具了
  15. 轻松Angularjs实现表格按指定列排序
  16. IDEA Spring注入显示红色波浪线
  17. 写vue项目时候 零星的笔记
  18. 设计模式之单例(GCD)
  19. redis 批量删除keys
  20. 【C++11】新特性 之 auto的使用

热门文章

  1. SQL Server 2014,表变量上的非聚集索引
  2. python内置模块(1)
  3. MySQL 备份与还原详解
  4. C#入门经典第五版之变量与表达式编码题训练
  5. Linux平台Qt creator报错:Circular all <- first dependency dropped
  6. 使用事务操作SQLite数据批量插入,提高数据批量写入速度,源码讲解
  7. Ext.NET 4.1 最新版本破解
  8. Python入门笔记(25):Python面向对象(2)
  9. Can you explain Lazy Loading?
  10. python中的__init__ 、__new__、__call__小结