1.

If You Want To Convert A Base Type To A Derived Type, What Type Of Conversion Do You Use?

Answer»

Explicit CONVERSION as shown below.
//Create a new derived type.
Car C1 = new Car();
// Implicit conversion to BASE type is safe.
Vehicle V = C1;

// Explicit conversion is REQUIRED to cast BACK to derived type. The CODE below will compile but throw an exception at run time if the right-side object is not a Car object.
Car C2 = (Car) V;

Explicit conversion as shown below.
//Create a new derived type.
Car C1 = new Car();
// Implicit conversion to base type is safe.
Vehicle V = C1;

// Explicit conversion is required to cast back to derived type. The code below will compile but throw an exception at run time if the right-side object is not a Car object.
Car C2 = (Car) V;



Discussion

No Comment Found