Hi, In working through a coding exercise to override the Object.clone() method I hit something I didn't expect. I thought that when accessing a field the declared type would always be used but when invoking a method, the actual type would be used. I overrode <code>public Object clone()</code> and invoked it on an object; which worked as expected. I then tried to print a field variable; which worked if I used an explicit cast BUT I also had to use an explicit cast when invoking an instance method. Here's a snippet of the code:
Can anyone explain why the getidNum() method needed to be cast when the actual type of veh2 was Vehicle_3?? Thanks. ------------------ Jane
Can anyone explain why the getidNum() method needed to be cast when the actual type of veh2 was Vehicle_3??
Because you have converted veh2 to the reference type Object. The Object class does not contain the method getidNum() and the compiler will flag this as an error. This would have worked if Object actually had a method called getidNum(). Though at runtime the overridden method from the actual object (veh2) would have been invoked due to late binding. However I don't understand why one could not assign veh2 to the Vehicle reference type when you used clone() .. anyone?? thanks, Zulfiqar.
[This message has been edited by zulfiqar raza (edited October 19, 2000).]
Jane Griscti What' your overriding clone() method?
Joined: Oct 04, 2000
The clone() method's return type is Object. You can not assign an object to Vehicle_3 class.
Joined: Aug 30, 2000
Hi, Here's the code for the clone()
As an override, it has to return the same type as the original method, an Object. I guess what's happening is the compiler knows veh2 is an Object at compile time. I was assuming that, for methods, the compiler didn't care what the type was; that only the Runtime checked it properly. Now I think that the compiler doesn't care about the cast, it assumes you know what your casting to, but if veh2 wasn't really a Vehicle_3 type at runtime I'd get a runtime error. Does that sound right? Thanks ------------------ Jane
Jane, Yes, your observations are right. The declared type may not be the runtime type of the object and hence you must use cast to "get back" your object type from the cloned object. That's why a lot of times the code that compiles correctly blows off at runtime. Since you declared <code>veh2</code> as <code>Object</code>, explicit casting was required to compile the code, it made the compiler to believe that you were actually dealing with an <code>Object</code> type instead of <code>Vehicle_3</code> type. I added flesh to your code( and did some reengineering )and came up with this little program. Note the code in blue. There is not much change from what you have done, but this should give you an idea how to implement cloning -
Hope that helps! Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Joined: Aug 30, 2000
Ajith, Thank you! Your code is much easier to understand. I got so wrapped up in the fact that I had to cast the method and how cumbersome that would be (and hard to remember ) it didn't occur to me that I could simply cast on the original assignment!!