Chapter 17
The Polymorphic Variable
A polymorphic variable is a variable that can reference more than one type of object. As a consequence, such a variable can maintain values of different types during the course of execution. In a dynamically typed language all variables are potentially polymorphic. In a statically typed language the polymorphic variable is the embodiment of the principle of substitution, a topic we examined in Chapter 8. Many of the examples we’ve seen that discussed the principle of substitution have used a simple assignment.
// assign a child value to a parent variable
Parent variable = new Child() ;
However, such examples are, in fact, relatively rare in practice. Most commonly, substitution ...
Get An Introduction to Object-Oriented Programming, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.