superclass, base class, parent class.
subclass, derived class, child class.

⚠️ 不要觉得 superclass 好像在说高类一等,或者包含的能力更多,其实恰恰相反。用这样的前缀其实来自于数学中“集合”的概念

关于什么时候用继承是合理的

  • 可以用 “is-a” rule 来判断。 (比如 manager 是 employee,反之不一定)
  • substitution principle. 就是说任何情况都可以用父类定义子类

Defining Subclasses

Use the Java keyword extends to denote inheritance.

public class Manager extends Employee {
	private double bonus;
	public void setBonus(double bonus) { this.bonus = bonus; }
}

感觉这代码读着 extends 后面缺个“from”

C++ Note review

Inheritance is similar in Java and C++. Java uses the extends keyword instead of the : token. All inheritance in Java is public inheritance; there is no analog to the C++ features of private and protected inheritance.

就算没有直接指明父类中成员,但是子类都能用,子类的父类用不了。也就是说只需要写出那些不同的地方就好了 这么明显的东西我为什么还要写这么长的笔记?

When designing classes, you place the most general methods in the superclass and more specialized methods in its subclasses.

Factoring out common functionality by moving it to a superclass is routine in object-oriented programming.

Overriding Methods

毕竟子还是发生了变化,所以有些方法父类的在子类就不对了,所以需要 override

⚠️ 注意 override 和 overload 之间的区别,如果同名方法,但是 signature 不一样,那其实是在重载,signature 也一样那才是重写。并且他们的 access level 也得是一样的

Attention

父父子子,各管各的 (毕竟),所以子没法直接获取父中定义的 fields (即便子确实也有父的这些 fields),因此获取得靠父提供的方法 怎么感觉这么不得劲呢…明明也是自己的 fields,但是却没法获取,看看 C++ 中是怎么说的吧

但是直接叫方法了也不对,因为 override 了,实际上是在叫自己,所以还要加个 super (如果没有 override 那确实是可以直接调用父类的方法,这里原因也确实在于 override)


总之 fields 不能共享,但是 methods 可以共享。fields 不能 override,methods 可以 override

public double getSalary() {
	double baseSalary = super.getSalary();
	return baseSalary + bonus; 
}

super is not as analogous to the this reference

super is not a reference to an object. You cannot assign the value super to another object variable.

super is a special keyword that directs the compiler to invoke the superclass method.

确实啊,它只是想要调用自己这个 obj 中父类给它的,所以 super 能指向谁?谁也指向不了

Subclass Constructors

特点决定子类构造方法必须 (间接或直接) 包含 super()。因为你总要对每一个 fields 赋初值吧,但是你又不能直接接触,那怎么办呢,用父类 public 的构造方法咯

⚠️ 并且 super() 必须放在第一句。如果压根没放,那也会默认叫了空的父类构造函数。如果父类没有空的构造函数,那就会报错

public Manager(String name, double salary, int year, int month, int day) {
	super(name, salary, year, month, day);
	bonus = 0; 
}

Here super is relate to this

不是说这里 super 就是 reference 了,而是==this 有两个意思,super 也有两个意思,他们第二个意思是类似的==

类似的,当用到第二个意思的时候,都要求去叫别的构造方法,而且都必须是第一句话

C++ Note review

In a C++ constructor, you do not call super, but you use the initializer list syntax to construct the superclass. The Manager constructor would look like this in C++:

// C++ 
Manager::Manager(String name, double salary, int year, int month, int day) : Employee(name, salary, year, month, day) {
	bonus = 0;
}

Inheritance Hierarchies

The collection of all classes extending a common superclass is called an inheritance hierarchy.

可以多个类继承自同一个父类,但是也只能来自同一个父类

子类也可以自己有自己的子类

同级子类之间没有关系

C++ Note review

In C++, a class can have multiple superclasses. Java does not support multiple inheritance. For ways to recover much of the functionality of multiple inheritance, see Section 6.1, “Interfaces,” on p. 296.