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.
You might also like to view...
There are exactly eight reference types available in Java.
Answer the following statement true (T) or false (F)
What advantage does a switch have over a hub?
What will be an ideal response?
The act of ________ is an intentional deception for the purpose of gaining something of value
Fill in the blank(s) with correct word
When you use the fill effects in the format data series dialog box, you can not Select one:
a. select a pattern b. select a background color c. rotate text on the chart d. select a fore ground color