what is the type of the object it references?

Given:
<code>
public class X implements Z {
public String toString() { return “I am X”; }
public static void main(String[] args) {
Y myY = new Y();
X myX = myY;
Z myZ = myX;
System.out.println(myZ);
}
}
class Y extends X {
public String toString() { return “I am Y”; }
}
interface Z {}
</code>
What is the reference type of myZ and what is the type of the object it references?

Given:

public class X implements Z {
public String toString() { return "I am X"; }
public static void main(String[] args) {
Y myY = new Y();
X myX = myY;
Z myZ = myX;
System.out.println(myZ);
}
}
class Y extends X {
public String toString() { return "I am Y"; }
}
interface Z {}

What is the reference type of myZ and what is the type of the object it references?

A.
Reference type is Z; object type is Z.

B.
Reference type is Y; object type is Y.

C.
Reference type is Z; object type is Y.

D.
Reference type is X; object type is Z.

Explanation:

Note: Because Java handles objects and arrays by reference, classes and array types are known as
reference types.



Leave a Reply 5

Your email address will not be published. Required fields are marked *


Kalil Peixoto

Kalil Peixoto

Why is not C? I guess the reference type is Z, as the algorythm was coded. Right?

sully

sully

Z myZ = myX;
i would choose C as well.
definatly wrong though because myZ doesn’t even mention Y

Oene Bakker

Oene Bakker

I also would go for answer C