• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Afroz Ahmed
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?.


 
Gunjan Sahay
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.
 
Ilja Preuss
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?
 
Peter Chase
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?
 
David Weitzman
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.
 
Joel Naten
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!