#753 – Implicitly Upcast to a Base Class Reference
January 8, 2013 Leave a comment
You can convert a reference to a child class to a reference to its base class using an upcast. (“Up” indicates that you’re moving up the inheritance tree). You can do this upcast implicitly, meaning that you don’t need a cast operator that indicates the target type.
For example, assume that Terrier is a subclass of Dog. If we have a reference to a Terrier object, we can assign that reference to a variable that is a reference to a Dog.
Terrier t = new Terrier("Jack", Terrier.GrowlFactor.Severe); // Implicit upcast - aDog now refers to Jack the Terrier Dog aDog = t;
After the upcast, we have a reference of type Dog that refers to an instance of a Terrier.