This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Why finalize() and clone() are protected in Object class  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

All the classes defaultly extends 'Object' class and inheritis all it's public and protected methods.Why is it needed that the finalize() and clone() methods were protected.(Is public not enough?).

By making protected,which are the classes(who are not extending Object and not getting finalize() method) to whome this method must not be available?.


 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Afroz,
I think the reason for making finalize() as protected is that all derived classes must call the base classes finalize() as well. If you make them anything else, chances are the that the derived classes may not invoke the base class' finalize() method.
Hence even making them public will not solve the purpose.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finalize() simply shouldn't be called from the outside at all - it is called by the JVM before an object.

clone() should only be called from the outside on classes which implement the Clonable interface. Those classes can then make the method public.

Does that help?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, why is clone() in Object at all? Why is it not in Cloneable?
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clone() is an odd function. One reason clone() is defined in the Object class is because that's a good private place to stick the default implementation (a shallow copy) without worrying about the security implications that would pop up if the default implementation were a static method in another class somewhere that could clone any class at all.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finalize() is in case you need to make sure something gets cleaned up in the class before it is freed. For instance a connection, or something like that. Normally, you will not need to use it.
 
Afroz Ahmed
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja Preuss

Thanks.
But I did not get "finalize() simply shouldn't be called from the outside at all"?Who can call from outside?
[ August 20, 2004: Message edited by: Afroz Ahmed ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!