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.