• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with declared type vs actual type

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).]
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane Griscti
What' your overriding clone() method?
 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clone() method's return type is Object.
You can not assign an object to Vehicle_3 class.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!

------------------
Jane
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic