This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

clone() and finalize()

 
sudhakarc kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why clone() and finalize() method in Object class is protected.While all the methods of Object class are public.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 52664
122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
His brain is the size of a cherry pit! About the size of this ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!