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