10.1 Dynamic Method Selection Puzzle (Online Only)
10.1.1 A Typing Puzzle
上来先整一道题做做:
Suppose we have two classes: 有两个类
- Dog: Implements bark() method.
Dog
实现了 bark()
- ShowDog: Extends Dog, overrides bark method.
ShowDog
继承了 Dog
,override 了 bark()
Summarizing is-a relationships, we have:
- Every ShowDog is-a Dog
- Every Dog is-an Object.
- All types in Java are a subtype of Object.
For each assignment, decide if it causes a compile error.
For each call to bark, decide whether:
- Dog.bark() is called,
- ShowDog.bark() is called, or
- A syntax error results.
The rules:
- Compiler allows memory box to hold any subtype.
- Compiler allows calls based on static type.