Managing Instance Types with species
Suppose a method—for example, clone()—of your class creates and returns an instance of its own type. Now, suppose you inherit from this class. Automatically a user of your derived class can reuse the clone() method defined in its base class. But when called on an instance of the derived class, should clone() return an object of the base type or the derived type?
In general, when using inheritance, if a method in the base class creates an instance, should that instance be of the base type or the derived type? It’s a mark of an educated mind to say “It depends!”
Sure, it may depend on the context, the application, and maybe several other things. But at the programming level we need to have a way to manage the ...
Get Rediscovering JavaScript 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.