Ryan Sykes wrote:
However, it struck me that there is a slight problem because if I tried to clone a Manager object, I would get a clone by virtue of the clone() method specified in the Employee class. While this would still do a perfectly fine job of performing a shallow copy of the original Manager object, it would return an object of type Employee
So I went ahead and tried writing the code and running it (see below), and it gave me an error unless I recast the cloned object as follows:
Personally, it seems like a bad idea to rely on inheritance for the implementation of the clone method because as in this case, you won't be sure of the actual Object type that you will be getting back
MySQL Blog
http://mysqlearner.blogspot.com/
Zeeshan Sheikh wrote:
Clone method is of type Employee & original instance is of type Manager which is a subclass of Employee so type casting is required otherwise it would be a mismatch. So for compiler to understand explicit casting is required.
Hope this helps.
Jeff Verdegan wrote:
Zeeshan Sheikh wrote:
Clone method is of type Employee & original instance is of type Manager which is a subclass of Employee so type casting is required otherwise it would be a mismatch. So for compiler to understand explicit casting is required.
Hope this helps.
The problem is that the OP mistakenly thought that the object produced was just an Employee, not a Manager.
Ryan Sykes wrote:my concern was more to do with what one actually expects a generic clone method in a class that implements or inherits the Cloneable interface to return.
For example, I would think that if I had no prior knowledge of how cloneable was implemented in Employee and Manager classes (just that both of them implement cloneable), then I would expect that:
would work, and original.clone() would return a copy of the object with type Employee.
I would naively think should also work (if I did not know how Manager actually inherits the clone() method).
Jeff Verdegan wrote:
I expect it to return the same class as the original object. And it does. And, knowing how Java works, without regard to clone() in particular, I wouldn't expect to get a reference of a subclass type without casting unless the subclass overrides the method itself.
Jeff Verdegan wrote:
For instance, List has a subList() method that returns List. Would you expect to call that on an ArrayList and be able to use the result as an ArrayList without a cast (even if the returned type were guaranteed to be the same type as the original List, which I don't think it is)? If so, can you please specify, clearly and precisely--preferably by citing the JLS--what part of the language would make that possible?
Or, for that matter, can you write a method other than clone() that behaves the way you're saying clone() should behave?
Ryan Sykes wrote:
Good point Jeff. I haven't gotten to Generics yet...so I was hoping there might be some magic there that could be used to make clone() behave the way I was thinking it should,
As a final clarification...am I missing some finer/subtle point regarding polymorphism/inheritance as to why it would be better for the inherited clone() method to return a cloned object of declared type of the superclass
PS - Just thought of this... would this work? return (getClass()) super.clone();
If Child doesn't override Parent's clone(), then the clone() method that we call will return a Child object, but, since it's implemented in Parent, it's declared to return Parent.
Now, we certainly could override clone() in Child, declare it to return Child, so that we don't have to cast.
Don't get me started about those stupid light bulbs. |