my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with declared type vs actual type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with declared type vs actual type" Watch "Problem with declared type vs actual type" New topic
Author

Problem with declared type vs actual type

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
zulfiqar raza
Ranch Hand

Joined: Oct 18, 2000
Posts: 81

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).]
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
Jane Griscti
What' your overriding clone() method?
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
The clone() method's return type is Object.
You can not assign an object to Vehicle_3 class.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with declared type vs actual type
 
Similar Threads
Question regarding Cloneable interface and Subclasses
Final variable
arraycopy() question
my notes on JLS for any1 who needs them !!
cast