#753 – Implicitly Upcast to a Base Class Reference

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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: