Reference type and object type
Asked Answered
M

6

14

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?!

Moneymaker answered 27/5, 2014 at 14:50 Comment(0)
B
4

Whats wrong here ?

Printed answer in your book/material is wrong here :p

Reference variable theMobile of type Mobile is referring to object of type Truck.

So answer 3 is correct, Reference type is Mobile and Object type is Truck.

You can check object type with theMobile.getClass() which will return Truck and reference type is what statically declared in your code, which is Mobile in your Mobile theMobile = ... declaration.

Bounce answered 27/5, 2014 at 15:11 Comment(0)
L
10

I've never seen those terms used for this, but I assume they mean declared type vs run time type.

Mobile theMobile = theVehicle;

The variable has a declared type of Mobile and a run time type of Truck. Answer C is correct.

The terms reference type refer to any type in Java that is not a primitive and not the null type.

Lorrimor answered 27/5, 2014 at 14:53 Comment(1)
The term "reference type" doesn't just refer to "compile-time" types. A variable of type Mobile[] may hold a reference to a one-element array of type Vehicle[], which would in turn hold a reference to a Truck. The type of reference stored in the array would be Vehicle, although the only type known at compile time would be Mobile.Appointee
B
4

Whats wrong here ?

Printed answer in your book/material is wrong here :p

Reference variable theMobile of type Mobile is referring to object of type Truck.

So answer 3 is correct, Reference type is Mobile and Object type is Truck.

You can check object type with theMobile.getClass() which will return Truck and reference type is what statically declared in your code, which is Mobile in your Mobile theMobile = ... declaration.

Bounce answered 27/5, 2014 at 15:11 Comment(0)
I
4

There is another thumb rule which is handy, and it goes something like this

Class_Name Reference_Variable = new Class_Constructor()

enter image description here

So the piece of code

Truck theTruck = new Truck();
Vehicle theVehicle = theTruck;
Mobile theMobile = theVehicle;

Here -

theTruck is the "reference variable" of type Truck and "Object Type" of Truck

theVehicle is the "reference varible" of type Vehicle and "Object Type" of theTruck "reference", which is, Truck

theMobile is the "reference variable" of type Moble and "Object Type" of theVehicle "reference", which is, Truck.

So Option C is the correct answer.

Ironsides answered 13/1, 2015 at 11:31 Comment(1)
Are you sure 'Object of Type' can refer to both the Declared type and also the Object type? I thought Object of Type would always refer to the object itself, thus the 'Object type'.Pavyer
R
3

theTruck is a Truck. Since Truck extends Vehicle, when you say Vehicle theVehicle = theTruck theVehicle is still a Truck. Same thing when you do Mobile theMobile = theVehicle: Your object is still a Truck.

EDIT: According to another answer, C is correct, because you're referring to your Truck as a Mobile.

Rampant answered 27/5, 2014 at 14:52 Comment(0)
G
1

Thumb Rule

a super class reference variable can be assign to subclass Object

According to that Rule Mobile theMobilecan be Assigned to vehicle or Truck

As vehicle is Not instantiated , it is also a reference type

So Answer 3 C Reference type is "Mobile", object type is "Truck" is correct

Gum answered 27/5, 2014 at 16:40 Comment(0)
M
0

Answer C. Reference type is "Mobile", object type is "Truck" is correct. While the reference points to a Truck object at the moment, it can be moved to point to any object that implements Mobile or a sub-interface thereof. Thus, the reference type is Mobile.

Marieann answered 27/5, 2014 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.