As I know, every clone() method provided by JDK returns Object. Why don't they return explicit type? I have to convert the type every time I use their clone().
What's the benefit of returning Object?
Why does all the clone() method return Object type?
Moderator: Board moderators
Why does all the clone() method return Object type?
I stay home. Don't call me out.
From what I remember about Java, every Java class implicitly extends the Object class, which is the base class of all Java classes. The Object class has a clone method which is inherited by any Java class, unless you specifically implement the clone method in the Java class you are writing to override the clone method in the Object class.
Every clone() method provided by JDK are overriden methods except the one in Object class, because the clone() method in Object is protected. If one class want to provide clone() method, it must override it. And my question is why don't the provide clone() methods which return explicit types.
I stay home. Don't call me out.
I never really understood the need for clone(), so I never used it, but I looked it up, best to read it for yourself:
http://mindprod.com/jgloss/clone.html
Btw, Roedy's glossary is an excellent resource for Java. I haven't checked it in a while, but he is probably keeping it up-to-date.
http://mindprod.com/jgloss/clone.html
Btw, Roedy's glossary is an excellent resource for Java. I haven't checked it in a while, but he is probably keeping it up-to-date.