Polymorphism is the third essential feature of an object-oriented programming language, after data abstraction and inheritance.
Upcasting revisited
Forgetting the object type
The twist
Method-call binding
Producing the right behavior
Extensibility
Polymorphism is an important technique for the programmer to “
separate the things that change from the things that stay the same.”
Pitfall: “overriding” private methods
The result of this is that only
non-private methods may be overridden, but you should watch out for the appearance of overriding private methods, which generates no compiler warnings, but doesn’t do what you might expect. To be clear, you should use a different name from a private base-class method in your derived class.
A private method is automatically final, and is also hidden from the derived class.
Pitfall: fields and static methods
Once you learn about polymorphism, you can begin to think that everything happens polymorphically.
However, only ordinary method calls can be polymorphic. For example, if you access a field directly, that access will be resolved at compile time.
If a method is static, it doesn’t behave polymorphically.
static
methods are
associated with the class, and not the individual objects.
Constructors and polymorphism
Constructors are not polymorphic (
they’re actually
static
methods, but the static declaration is implicit)
Order of constructor calls
This means
The order of constructor calls for a complex object is as follows:
1. The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.
The order of constructor calls for a complex object is as follows:
2. Member initializers are called in the order of declaration.
3. The body of the derived-class constructor is called.
Inheritance and cleanup
Behavior of polymorphic methods inside constructors
The only safe methods to call inside a constructor are those that are final in the base class. (This
also applies to private methods, which are automatically final.)
also applies to private methods, which are automatically final.)
The actual process of initialization is:
- The storage allocated for the object is initialized to binary zero before anything else happens.
- The base-class constructors are called as described previously. At this point, the overridden draw( ) method is called (yes, before the RoundGlyph constructor is called), which discovers a radius value of zero, due to Step 1.
- Member initializers are called in the order of declaration.
- The body of the derived-class constructor is called.
Covariant return types
Designing with inheritance
A general guideline is “Use inheritance to express differences in behavior, and fields to
express variations in state.”
express variations in state.”
Substitution vs. extension
Downcasting and runtime type information