This week's book giveaway is in the Testing forum. We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line! See this thread for details.
�Given a scenario, develop code that demonstrates the use of polymorphism. Further, determine when casting will be necessary and recognize compiler vs. runtime errors related to object reference casting. =======my notes ================
Polymorphism is sometimes used as a synonym for dynamic binding but they are not the same. Polymorphism is a feature of JAVA that allows an object reference variable of a superclass to hold the object reference of any of its subclasses. Dynamic binding is a term used to refer to the runtime system selecting the proper method to use when it executes a method call. If an object variable can hold references for different classes then the class stored in the variable or its methods are not know until the moment execution occurs.
At compile time if the compiler detects an invalid cast then an exception is thrown. If the validity of a cast cannot be determined then the runtime system will catch the errors at runtime. Casting up the inheritance hierarchy is valid. This means a superclass object reference variable can hold any of its subclass objects. Casting down the inheritance will not work unless an explicit cast is used and then it depends on the validity of the cast. In some cases the type or class of a variable will be changed or forgotten by the runtime system and it will be necessary to cast it back to the original class.
Without looking it up I think they meant an object reference variable that gets cast to another type then no longer is of the original type and to place an object of the original type back into it you need to do an explicit cast. This may not be the example they used in the book. Somehow JAVA forgets the original type .
I think they also said arrays remember their original type so this is not a problem for arrays.
Instead of saying that explicit casting is needed because the runtime environment "forgets" a variable's type, I would say it's needed because the compiler cannot be certain of the object's true type. Explicit casting is the programmer's way of "reassuring" the compiler that what you're doing will be okay at runtime.
Arrays are objects in their own right. But it's important to recognize what they hold are references -- not objects themselves. The type of an array is determined by the type of references it holds. For example, a "String array" is an array that holds references to Strings. The reference to the array itself can be treated like any other reference. For example...
Object objArray = new String; //upcast of array type String strArray = (String)objArray; //downcast with explicit cast
In this case, the variable objArray has a type of "Object array," but the object itself is really a String array. This object (the array) holds references to Strings.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Don't play dumb with me! But you can try this tiny ad: