I'm mentoring a colleagues OCA-Java 7 Certification. He's also attending a course and did a preparation exam there. One of the questions was about reference and object types. Here is the code:
package com.company;
public class Vehicle implements Mobile {
public static void main(String[] args) {
Truck theTruck = new Truck();
Vehicle theVehicle = theTruck;
Mobile theMobile = theVehicle;
}
}
class Truck extends Vehicle {
}
interface Mobile {
}
The question: What is is the reference type and the object type of theMobile
?
And here are the choices:
- A Reference type is "Mobile", object type is "Mobile"
- B Reference type is "Truck", object type is "Truck"
- C Reference type is "Mobile", object type is "Truck"
- D Reference type is "Car", object type is "Mobile"
Answer B is marked as the correct answer...but IMHO answer C is right. Who's wrong here?!
Mobile[]
may hold a reference to a one-element array of typeVehicle[]
, which would in turn hold a reference to aTruck
. The type of reference stored in the array would beVehicle
, although the only type known at compile time would beMobile
. – Appointee