Discuss four ways in which you can assign base-class and derived-class references to variables of base-class and derived-class types.

What will be an ideal response?

1) Assigning a base class reference to a base class variable. 2) Assigning a derived class reference to a derived class variable. 3) Assigning a derived class object’s reference to a base class variable is safe, because the derived class object is an object of its base class. However, this reference can be used to refer only to base class members. If this code refers to derived-class-only members through the base class variable, the compiler reports errors. 4) Attempting to assign a base class object’s reference to a derived class variable is a compilation error. To avoid this error, the base class reference must be downcast to a derived class type explicitly. At execution time, if the object to which the reference refers is not a derived class object, an exception will occur. The is operator
can be used to ensure that such a cast is performed only if the object is a derived class object. You can also use the as operator, which will return null of the object is not the correct type.

Computer Science & Information Technology

You might also like to view...

The SmartArt graphic type that shows proportional relationships is the ________

A) Hierarchy B) Pyramid C) Matrix D) Process

Computer Science & Information Technology

An icon set can make data visually easier to interpret

Indicate whether the statement is true or false

Computer Science & Information Technology