This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
When you downcast, what is really happening here: SubClassRef = (Subclass)SuperClassRef; Isn't the compiler and VM just checking to see if the cast rules are being followed? Many people make the analogy to Primitive casting arguing that since the Primitive variable will refer to a new value in a widening or narrowing conversion, a reference variable will likewise refer to new or different object when it is cast. Is an object actually being changed??? That seems dubious.
[This message has been edited by herb slocomb (edited November 02, 2001).]
When you downcast, what is really happening here: SubClassRef = (Subclass)SuperClassRef;
The compiler checks to see whether SuperClassRef is in the hirarchy of Subclass. The JVM checks whether or not the SuperClassRef is actually a class of type Subclass before it assigns it to the SubClassRef. If not, it throws a ClassCastException.
It does not change the actual type of any class. The class's class Class (confusing ? ) keeps track of what the actual class type of the object is.
[This message has been edited by Marilyn deQueiroz (edited November 02, 2001).]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Feb 12, 2001
Assuming the Subclass has additional methods and variables that the Superclass does not, when you do this: SuperclassRef = SubclassRef; The additional methods and variables of the Subclass object still exist yet cannot be referenced by the superclass variable. To use a bad analogy with Primitives, is this like "overflow" where the Superclass variable simply can't "hold" all the memory addresses for all the additional variables and methods? I'm not clear how a reference variable references an object , and how it references methods and variables of an object. Apparently, a reference variable does somehow reference not an object, but a collection of inidividual methods and variables which we call an object for convenience. ???
Originally posted by herb slocomb: The additional methods and variables of the Subclass object still exist yet cannot be referenced by the superclass variable.
Actually the methods are all called based on the underlying object. Check out this code:
The results are: method in Test method in SubTest method in SubTest So even though you changed the variable t to reference an object of the subclass the subclasses methods are still called. This is all called overriding. Shadowing on the other hand applies to static variables and methods as well as instance variables. In shadowing the variable or static method called is based on the type of the variable not the object referenced. This code shows shadowing:
the output is: i: 1 i: 2 i: 1 Want some food for thought? Change the code above so that the method method() prints out the value of i. Then call that method after you assign the subclass variable to the base class variable - what value of i gets printed?
I'm not clear how a reference variable references an object , and how it references methods and variables of an object. Apparently, a reference variable does somehow reference not an object, but a collection of inidividual methods and variables which we call an object for convenience. ???
The variable actually holds a pointer to the memory location of the object in question. Each object has a pointer to a table of their methods. So when you are passing around variables basically your just passing around memory addresses. ther is more to it then that but is a nice neat way of looking at it. hope that helped
------------------ Dave Sun Certified Programmer for the Java� 2 Platform
Joined: Feb 12, 2001
So the refernce variable is a pointer to the object and the object has a pointer to its methods. Interesting. Thanks to all for your insight on this. I think I got it now.