This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this roundup question trips me up every time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this roundup question trips me up every time" Watch "this roundup question trips me up every time" New topic
Author

this roundup question trips me up every time

donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
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

Joined: Oct 02, 2003
Posts: 11143
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
"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

Joined: Mar 12, 2003
Posts: 75
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

Joined: Mar 12, 2003
Posts: 75
How about class's having a tier value...?



I'm done now.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11143
    
  16

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

Joined: Mar 12, 2003
Posts: 75
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

Joined: Mar 12, 2003
Posts: 75
"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

Joined: Oct 02, 2003
Posts: 11143
    
  16

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

Joined: Mar 12, 2003
Posts: 75
"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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this roundup question trips me up every time
 
Similar Threads
class / subclass.....
class/ subclass
Rules Roundup game
DownCasting Generally
Please explain these answers