posted 22 years ago
Well, your questions are simple and basic, you need to read more about reference casting. I'll try to give you a brief, but I strongly recommend that you read in details and more in depth about casting.
First, you have to know the difference between Reference type and object type:
if C extends B, B extends A, then:
B refB = new C();
refB has a reference type B and object runtime type C.
1- You can NOT cast an object of object runtime type B to a refernce type C even with explicit casting. Upcasting (object runtime wise) is not allowed. It will compile fine but will throw a runtime exception "ClassCastException", that�s because at compile time the compiler doesn�t know what is the actual runtime class of the object.
2- In case of B refB = new C(); the reference refB runtime type is C, so C refC = (C) refB; is legal, since you assign the reference of type C to an object of actual runtime type C as well.
3- In all cases, A refA = refB; & A refA = refC; is always accepted, downcasting is always permited without even explicit casting.
4- A refA = new C(); B refB = (B) refA; is also accepted it's downcasting from type reference A to type reference B, since the actual runtime type of refA is actually type C and C is a subclass of B.
5- 1.There is a difference between members overriding and members hiding. Member Variables and Static Methods are hidden, while Instance Methods are overridden. If an object of a subclass is instantiated with a super class reference the difference appears between hiding and overriding.
On calling members using the �Super� Object Reference of a �Sub� Object the value of the Member Variables and the implementations of the Static Methods in the Super Class are used, while the implementations of the Instance Methods in the Subclass are used.
6- 2.If a method that is not overridden or hidden is called on an object of a subclass � no matter what the reference type is �, the implementation of this method is in the body of the super class of course. If this method calls a method that is hidden (static) in the subclass, the implementation of the hidden method in the super class is used. But if the method � the first one � calls a method that is overridden (instance) in the subclass, the implementation of the overridden method in the subclass is used, having in mind that the object is of the subclass and no matter what reference type it is.
7- 3.If an object of a subclass is instantiated with a reference of a super class and a method that is defined in the subclass but not the super class is invoked on the object, a Compile-time error appears (Can�t resolve symbol), that�s because at compile time the compiler doesn�t know what is the actual runtime class of the object and so has to make sure the method exists in the definition of the object reference type class.
read more on inheritance and casting.
HTH
[ October 19, 2002: Message edited by: Alfred Kemety ]
Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true