File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes clone() and finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "clone() and finalize()" Watch "clone() and finalize()" New topic
Author

clone() and finalize()

sudhakarc kumar
Greenhorn

Joined: Feb 23, 2011
Posts: 4
why clone() and finalize() method in Object class is protected.While all the methods of Object class are public.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Unlike the other methods in Object, neither are meant to be called directly on an instance of Object, they are intended to be overridden by subclasses.

By default clone() throws an exception if the class does not implement Cloneable. If you want to make a class cloneable, the standard pattern is to implement Cloneable and override clone(), making it public.

finalize() is not meant to be called by the programmer - it's called by the garbage collector. So there's no need for it to be public.
sudhakarc kumar
Greenhorn

Joined: Feb 23, 2011
Posts: 4
i am not clear about the answer.
i want to know that what would have happened if these methods would have been public.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
If you search, you will find this question comes up frequently. The clone() method is not meant to be called until it has been overridden; then it can become public. The finalize() method should never be called; only the JVM should call it, so it does not need to be public.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: clone() and finalize()
 
Similar Threads
why only clone method and finalize method in object class are protected?
Doubt in the class Object
Public Vs Protected methods in object class
Why finalize() and clone() are protected in Object class
Java Object class