Chapter 7 - Inheritance Flashcards
1
Q
inheritance
A
- define a new class (subclass) in terms of how it extends an existing base class (superclass)
- using keyword ‘extends’
2
Q
runtime type vs. compile-time (static) type
A
- runtime: mentioned in new operation and represented by object returned by method getClass(); remains unchanged
- static type: type of variable in which a reference to the object is stored
3
Q
downcasting
A
- use instanceOf or getClass() before downcasting a variable
4
Q
single inheritance
A
- a given class can only declare to inherit from a single class
- by default, classes extend Object class
5
Q
inheriting fields
A
- when creating a new object, the object will have a field for each field declaration in its superclasses
- for fields to be accessible to subclasses, set their access modifiers to protected or use a getter method
6
Q
inheriting methods
A
- methods of a superclass are applicable to instances of a subclass
- can override methods
- can use super keyword to call method from superclass
7
Q
abstract classes
A
- cannot be instantiated
- can have both abstract (no implementation) and regular methods
8
Q
TEMPLATE METHOD design pattern
A
- put all common code in the superclass and define some hooks to allow subclasses to provide specialized functionality where needed
- declare template method to be fine (cannot be overidden by subclasses)
- the method implemented by subclasses is protected and abstract
9
Q
final classes
A
- cannot be inherited