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)的更多相关文章

  1. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  2. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  3. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  4. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  5. servlet(6) - servlet总结 - 小易Java笔记

    垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...

  6. Java笔记 —— 继承

    Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...

  7. Java笔记 —— 方法重载和方法重写

    Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...

  8. Java笔记 —— 初始化

    Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...

  9. Java笔记 —— this 关键字

    Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...

  10. Java 笔记 —— java 和 javac

    Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...

随机推荐

  1. P2P的理解

    B2C是Business-to-Customer的缩写,而其中文简称为“商对客”.“商对客”是电子商务的一种模式,也就是通常说的商业零售,直接面向消费者销售产品和服务.这种形式的电子商务一般以网络零售 ...

  2. js库写法

    前言: 现在javascript库特别多,其写法各式各样,总结几种我们经常见到的,作为自己知识的积累.而目前版本的 JavaScript 并未提供一种原生的.语言级别的模块化组织模式,而是将模块化的方 ...

  3. ccc array

    setInterval可以用来设置函数的执行频率 nodeList: { default:[], type:[cc.Node] } active 可以用来设置是否启用 cc.Class({ exten ...

  4. sdoi 2009 & 状态压缩

    是不是平时在手机里玩吃豆豆游戏玩腻了呢?最近MOKIA手机上推出了一种新的围豆豆游戏,大家一起来试一试吧. 游戏的规则非常简单,在一个N×M的矩阵方格内分布着D颗豆子,每颗豆有不同的分值Vi.游戏者可 ...

  5. 显式Intent和隐式Intent

    http://blog.csdn.net/qs_csu/article/details/7995966 对于明确指出了目标组件名称的Intent,我们称之为“显式Intent”. 对于没有明确指出目标 ...

  6. OpenSceneGraph学习笔记

    VirtualPlanetBuilder编译方法 转自:http://www.boyunjian.com/do/article/snapshot.do?uid=7327932418831703800 ...

  7. 前端自动化工具 -- fis 使用简介

    https://github.com/fex-team/fis FIS入门: http://fis.baidu.com/docs/beginning/getting-started.html FIS ...

  8. [转]as3事件流机制彻底理解

    题记: 看过网上一些as3事件流的教程,觉得大多都讲得不甚清楚,让人不能直观的理解事件流.而这篇教程以将事件流过程比喻成捕鱼过程,形象简单. 在此基础上对于as3事件流总算有了全面的理解.事件流机制说 ...

  9. TLV简介

    引子: 前段时间在项目中第一次接触TLV,项目中用这种格式来传输图片,语音等. 关于TLV TLV是一种可变的格式,意为:Type类型, Lenght长度,Value值.Type:该字段是关于标签和编 ...

  10. JQuery小结

    一.选择网页元素 jQuery的基本设计和主要用法,就是"选择某个网页元素,然后对其进行某种操作".这是它区别于其他函数库的根本特点. 使用jQuery的第一步,往往就是将一个选择 ...