There is a mistake in 1. 1. beagle b2=(beagle)dog1; 2. beagle b3=(beagle)dog2;
1. will cause a ClassCastException. It will compile OK because all the compalier is interested in when assessing casting is that the 2 classes are from the same hierarchy. (beagle and dog are) But at run time the real class of the object referenced by dog1 will be assessed. dog1 points to object of class dog. Can class dog be converted to beagle? NO, because it is trying to convert down to a subclass, rather than to superclass which is allowed.
2. Again no problem at compile time, as above. At run time JVM will see that in fact dog2 point to object of class beagle so the conversion is successfully done.
SCJP 5.0, SCWCD, SCBCD, Oracle Certified Master Enterprise Architect