Page 1 of 1
					
				About Question com.enthuware.ets.scjp.v6.2.207 :
				Posted: Tue Mar 19, 2013 8:08 am
				by ETS User
				Please convince me why b1 = (B1) b; will fail at run time.  It seems to be a legitimate downcast.
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.207 :
				Posted: Tue Mar 19, 2013 9:44 am
				by admin
				The actual object being pointed to by b at runtime is of class B. B is not B1. So you can't assign it to a reference of class B1.
You might want to try it out.
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.207 :
				Posted: Tue Nov 12, 2013 10:18 am
				by liemkit
				Am I correct in thinking this is about the reference variable itself? 
b1 has type B1 and, even though you cast to B1, b is still of type B. 
You cannot change the type of an reference variable.
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.207 :
				Posted: Tue Nov 12, 2013 11:18 am
				by admin
				This is a about the actual object that is referred to by the variable. You can't cast a base class object to a subclass object. For example, you can't cast Employee object to CEO object (assuming that CEO extends Employee). You can use a reference variable of type Employee to refer to a CEO though. 
Employee e = new CEO(); //valid
CEO ceo = (CEO) e; //valid because e points to an object that is a CEO.
Employee e2 = new Employee(); //valid
ceo = (CEO) e2;// Invalid - Will fail at run time - because e2 points to an object of class Employee, which is is not a CEO.
HTH,
Paul.