Can any one tell me How to clone an Object of a class?.I have tried to clone an object of a class using clone() method in Object class and it's throwing clone not supported exception.One more thing the clone method returns an object which is a protected object.How will I use that in a different class??. Any sample codes would be appriciatable. Thanks in Advance, Murali
"Excellence is never an accident; it is always the result of high intention, sincere effort, intelligent direction, skillful execution, and the vision to see obstacles as opportunities"
Your class needs to implement the Cloneable interface. Cloneable is simply a marker interface -- i.e. it does not have any methods. Also the clone() method in Object class is protected. So if you want the clone() method for your class to be publicly accessible from anywhere, you can override this method to be public. However be sure to call super.clone() from your overriden clone() method in order to create the correct Object type. For eg:
However remember that the Object clone() method creates a shallow copy, i.e. if your class has any object references, only the object references are cloned, not the objects themselves. [ May 14, 2002: Message edited by: Junaid Bhatra ]