面向对象的编程
7 面向对象的编程
Basic concepts: object, class, attribute, and method
Class variable 类成员变量: a variable associated with the class rather than with an instance of the class. You can also associate methods with a class—class methods类方法
Methods and variables that are not class methods or class variables are known as instance methods 实例方法 and instance variables 实例成员变量.

Interface
- Interface和Class: 定义和实现ADT
- 接口之间可以继承与扩展
- 一个类可以实现多个接口(从而具备了多个接口中的方法)
- 一个接口可以有多种实现类
接口:确定ADT规约;类:实现ADT
也可以不需要接口直接使用类作为ADT,既有ADT定义也有ADT实现
实际中更倾向于使用接口来定义变量
Using static factory instead of constructor
接口中的每个方法在所有类中都要实现,可以通过default方法,在接口中统一实现某些功能,无需在各个类中重复实现它。

Inheritance and Overriding
Overriding
Rewriteable Method: A method which allow a re-implementation.
Strict inheritance 严格继承:子类只能添加新方法,无法重写超类中的方法 (用final修饰)
Strict inheritance

Overriding
Method overriding is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.
重写的函数:完全同样的signature
实际执行时调用哪个方法,运行时决定。

如果父类型中的某个函数实现体为空 意味着其所有子类型都需要这个功能,但各有差异,没有共性,在每个子类中均需要重写。
When a subclass contains a method that overrides a method of the superclass, it can also invoke the superclass method by using the keyword super.
Bad Use of Overwriting Methods: 重写的时候,不要改变原方法的本意
Abstract Class
Abstract method:
- A method with a signature but without an implementation (also called abstract operation)
- Defined by the keyword abstract
Abstract class: A class which contains at least one abstract method is called abstract class
Interface: An abstract class which has only abstract methods
- An interface is primarily used for the specification of a system or subsystem. The implementation is provided by a subclass or by other mechanisms.
如果某些操作是所有子类型都共有,但彼此有差别,可以在父类型中设计抽象方法,在各子类型中重写
所有子类型完全相同的操作,放在父类型中实现,子类型中无需重写。
有些子类型有而其他子类型无的操作,不要在父类型中定义和实现,而应在特定子类型中实现。
Polymorphism, subtyping and overloading
Three Types of Polymorphism
Ad hoc polymorphism (特殊多态):when a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations.
Parametric polymorphism (参数化多态): when code is written without mention of any specific type and thus can be used transparently with any number of new types.(often known as generics or generic programming 泛型)
Subtyping (also called subtype polymorphism or inclusion polymorphism 子类型多态、包含多态): when a name denotes instances of many different classes related by some common superclass.
Ad hoc polymorphism and Overloading
Ad-hoc polymorphism is obtained when a function works on several different types (which may not exhibit a common structure) and may behave in unrelated ways for each type.

重载:多个方法具有同样的名字,但有不同的参数列表或返回值类型
价值:方便client调用,client可用不同的参数列表,调用同样的函数
Overloading is a static polymorphism
- 根据参数列表进行最佳匹配
- 静态类型检查
- 在编译阶段时决定要具体执行哪个方法
- 与之相反,overridden methods则是在run-time进行dynamic checking!
Overloading rules:
- MUST change the argument list. 不同的参数列表
- CAN change the return type. 相同/不同的返回值类型
- CAN change the access modifier. 相同/不同的 public/private/protected
- CAN declare new or broader checked exceptions. 异常
- A method can be overloaded in the same class or in a subclass. 可以在同一个类内重载,也可在子类中重载
Overriding vs. Overloading

Parametric polymorphism and Generic programming
Parametric polymorphism is obtained when a function works uniformly on a range of types; these types normally exhibit some common structure.
Generic programming is a style of programming in which data types and functions are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.
Suppose we want to implement the generic Set interface.
- 泛型接口,非泛型的实现类
- 泛型接口,泛型的实现类

Subtyping Polymorphism

Benefits of inheritance/subtype: Reuse of code, Modeling flexibility
In Java: Each class can directly extend only one parent class; A class can implement multiple interfaces.
“B is a subtype of A” means “every B is an A.”
In terms of specifications: “every B satisfies the specification for A.”
If you declare a subtype in Java (e.g., implementing an interface), then you must ensure that the subtype’s spec is at least as strong as the supertype’s.子类型的规约不能弱化超类型的规约。
子类型多态:不同类型的对象可以统一的处理而无需区分
Each object behaves according to its type (e.g., if you add new kind of account, client code does not change) 从而隔离了“变化”
instanceof
Operator that tests whether an object is of a given class

Advice: avoid instanceof() if possible, and never(?) use instanceof() in a superclass to check type against subclass.
Some important Object methods in Java
Overriding Object method
- equals() – true if the two objects are “equal”
- hashCode() – a hash code for use in hash maps
- toString() – a printable string representation
toString() – ugly and uninformative
You know what your object is so you can do better
Always override unless you know in won’t be called
- equals & hashCode – identity semantics
- You must override if you want value semantics
- Otherwise don’t
Designing good classes
Advantages of immutable classes
- Simplicity
- Inherently Thread-Safe
- Can be shared freely
- No need for defensive copies
- Excellent building blocks
How to write an immutable class
- Don’t provide any mutators
- Ensure that no methods may be overridden
- Make all fields final
- Make all fields private
- Ensure security of any mutable components (avoid rep exposure)
- Implement toString(), hashCode(), clone(), equals(), etc.
Q: When to make classes immutable?
A: Always, unless there’s a good reason not to
Always make small “value classes” immutable!