Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this roundup question trips me up every time

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given: Y is a subclass of X

X myX = new X();
Y myY = (Y)myX;

Answer: Yes

Explaination: It will compile. As far as the compiler is concerned myY might have been a X object all along; however, it will fail at runtime.

I always put No.

How could the object myY might have been a X object all along? Y is a subclass of X.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple answer: the compiler is dumb.

Slightly longer answer: the compiler is dumb. It doesn't remember all the details about every variable you declare, or object you create. so when you get to the second line, it says "well, I know myX is a type X. I also know that Y is a child class of X, so it it POSSIBLE that the myX variable was, at some point, a Y. The user thinks it was a Y (because he/she wrote a cast), so I'll allow it".

the compiler doesn't actually HAVE the objects. They don't get created until run-time. So, the compiler can't ask the object if it really is a Y. The run-time environment, however, can and does.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"so it it POSSIBLE that the myX variable was, at some point, a Y."

The compiler is assumming myX was at some point a Y type?

when it sees "(Y)myX"

even though in the prior line it sees "X myX = new X())" knows
that Y is a child of X?
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler does not know where objects are in relation to each other on the
hierarchy, but at runtime the JVM does as an object is being built.

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about class's having a tier value...?



I'm done now.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is assumming myX was at some point a Y type?

I wouldn't put it like that.
even though in the prior line it sees

i wouldn't put it like that either.

I am not sure the compiler ever "sees what was on the previous line", but I am not a compiler expert. what i think happens is the compiler sees the line

Y myY = (Y)myX;

and says "well, i do remember that myX is a reference to a class X. I also know that class Y are derived from class X. It is, therefore, possible that the object currently referred to by myX is actually a class Y, so I'll allow this".

If, however, you tried to cast your myX to something it could never be - something not in it's inheritance path, then you WILL get a compiler error.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The light bulb shined when you said the object that myY refers to might be a
a class of type Y. I get it, I think.

thank you!
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"It is, therefore, possible that the object currently referred to by myX is actually a class Y, so I'll allow this"

How would it be possible?

this is a guess (I hope I'm following you):

X myX = new X();
Y myY = new Y();
myX = myY;
Y myY = (Y)myX;
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. let's try this...



now look at it. all I know is that myX must refer to a type X, or one of it's subclasses. the compiler looks at this and says "well, the object referred to by myX could be any of a bunch of things, but it MUST be either an X or one of it's subtypes. it is possible that the type is a Y, and that is a legal cast. OK!!!".

Now what if you had this (still with Y being a subclass of X):



the compiler will complain. it is impossible for a String to be a Y. the compiler knows this, and say "nope!! sorry!!!".
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"or one of its subclass"

This is because:

A object reference of a certain type(myX)can refer to its class type(X) or a subclass of its type(Y):

Y is a subclass of X,
myX is of type X,
myY is of type Y,

now:

myX can legally refer to a subclass type of X(in this case Y) because it
has all of the methods(guts) of class X.

I've got it.

And i know, i know this is was you have been saying all along.

thank you soooooo much

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic