Thinking in Java——笔记(9)
Polymorphism
Abstract classes and methods
- If you have an abstract class, objects of that specific class almost always have no meaning.
- You create an abstract class when you want to manipulate a set of classes through its common interface.
- A class containing abstract methods is called an abstract class.
- It cannot safely create an object of an abstract class, so you get an error message from the compiler.
- If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don’t, then the derived class is also abstract.
- It’s possible to make a class abstract without including any abstract methods. you want to prevent any instances of that class.
- Making a class abstract doesn’t force you to make all the methods abstract.
- Abstract classes are also useful refactoring tolls, since they allow you to easily move common methods up the inheritance hierarchy.
Interfaces
- The abstract keyword allows you to create one or more undefined methods in a class.
- The interface keyword produces a completely abstract class, one that provides no implementation at all.
- The interface is used to establish a "protocol" between classes.
- It allows you to perform a variation of "multiple inheritance" by creating a class that can be upcast to more than one base type.
- An interface can also contain fields, but these are implicitly static and final.
- Once you’ve implemented an interface, that implementation becomes an ordinary class that can be extended in the regular way.
- When you implement an interface, the methods from the interface must be defined as public.
- It doesn’t matter if you are upcasting to a "regular" class, an abstract class, or to an interface.
Complete decoupling
- Creating a method that behaves differently depending on the argument object that you pass it is called the Strategy design pattern.
- The method contains the fixed part of the algorithm to be performed, and the Strategy contains the part that varies.
- In this approach to Adapter, the Adapter constructor takes the interface that you have, and produces an object that has the interface that you need.
“Multiple inheritance” in Java
- This act of combining multiple class interfaces is called multiple inheritance.
- In Java, you can perform the same act, but only one of the classes can have an implementation.
- If you do inherit from a non-interface, you can inherit from only one. All the rest of the base elements must be interfaces.
- You can upcast to each interface, because each interface is an independent type.
- When you combine a concrete class with interfaces this way, the concrete class must come first, then the interfaces.
- To prevent the client programmer from making an object of this class and to establish that it is only an interface.
- If it’s possible to create your base class without any method definitions or member variables, you should always prefer interfaces to abstract classes.
Extending an interface with inheritance
- Normally, you can use extends with only a single class, but extends can refer to multiple base interfaces when building a new interface.
Name collisions when combining Interfaces
- Using the same method names in different interfaces that are intended to be combined generally causes confusion in the readability of the code, as well.
Adapting to an interface
- You write a method that performs certain operations, and that method takes an interface that you also specify.
- It means that a method that takes an interface provides a way for any class to be adapted to work with that method.
Fields in interfaces
- Any fields you put into an interface are automatically static and final.
- Interface is a convenient tool for creating groups of constant values.
Initializing fields in interfaces
- Fields defined in interfaces cannot be "blank finals," but they can be initialized with non-constant expressions.
- Since the fields are static, they are initialized when the class is first loaded, which happens when any of the fields are accessed for the first time.
- The fields, of course, are not part of the interface. The values are stored in the static storage area for that interface.
Nesting interfaces
- Implementing a private interface is a way to force the definition of the methods in that interface without adding any type information.
- An interface nested within another interface is automatically public and cannot be made private.
- When you implement an interface, you are not required to implement any interfaces nested within.
- private interfaces cannot be implemented outside of their defining classes.
Interfaces and factories
- A typical way to produce objects that fit the interface is the Factory Method design pattern.
- Instead of calling a constructor directly, you call a creation method on a factory object which produces an implementation of the interface.
- In this way, in theory, your code is completely isolated from the implementation of the interface, thus making it possible to transparently swap one implementation for another.
- Why would you want to add this extra level of indirection? One common reason is to create a framework.
Summary
- Almost anytime you create a class, you could instead create an interface and a factory.
- Any abstraction should be motivated by a real need.
- An appropriate guideline is to prefer classes to interfaces.
- Start with classes, and if it becomes clear that interfaces are necessary, then refactor.
Thinking in Java——笔记(9)的更多相关文章
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- servlet(6) - servlet总结 - 小易Java笔记
垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...
- Java笔记 —— 继承
Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...
- Java笔记 —— 方法重载和方法重写
Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...
- Java笔记 —— 初始化
Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Java 笔记 —— java 和 javac
Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...
随机推荐
- Centos搭建Linux测试环境,几个基本的设置项
一.添加sudo 权限 1.su - 切换至root,需要rootmim 2.修改 /etc/sudoers 的权限 chmod 644 /etc/sudoers 3.vi /etc/sudoers ...
- Java API 实现HBase的数据添加与过滤查询
包依赖比较麻烦,找了好久,我用的CDH5.0 现将所依赖的包的列表清单如下: public class EmployeeDao { /** * @param args */ public static ...
- Redis Tools
1. Resources Redis Desktop Manager http://redisdesktop.com/ Redis命令的中文文档 http://redisdoc.com/ Redis安 ...
- Storm TimeCacheMap RotatingMap源码分析
TimeCacheMap是Twitter Storm里面一个类, Storm使用它来保存那些最近活跃的对象,并且可以自动删除那些已经过期的对象. 不过在storm0.8之后TimeCacheMap被弃 ...
- BOM和DOM的区别和关联
BOM 1. BOM是Browser Object Model的缩写,即浏览器对象模型. 2. BOM没有相关标准. 3. BOM的最根本对象是window. 从1可以看出来:BOM和浏览器关系密切. ...
- java-嵌套类
浏览以下内容前,请点击并阅读 声明 java允许在一个类中定义另外一个类,这就叫类嵌套.类嵌套分为两种,静态的称为静态嵌套类,非静态的又称为内部类. 使用嵌套类的原因: 能够将仅在一个地方使用的类合理 ...
- Struts2上传大小限制
Struts中报错 the request was rejected because its size (***) exceeds the configured maximum (2097152) 最 ...
- UVa 3487 & 蜜汁建图
题意: 有两家公司都想向政府申请某些资源的使用权,并且他们都提供了一些申请列表,列表中含有申请费用和资源种类,同一家公司的申请列表之间不含有重复的资源.政府只可以完整地接受和拒绝谋一份申请列表,问政府 ...
- topcoder SRM 618 DIV2 LongWordsDiv2
此题给出的条件是: (1)word的每个字母都是大写字母(此条件可以忽略,题目给的输入都是大写字母) (2) 相等字符不能连续,即不能出现AABC的连续相同的情况 (3)word中不存在字母组成xyx ...
- 51Nod 1002 数字三角形 Label:水水水 && 非学习区警告
一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上. 5 8 4 3 6 9 7 ...