aspose file tools
The moose likes Java in General and the fly likes Why finalize() and clone() are protected in Object class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why finalize() and clone() are protected in Object class" Watch "Why finalize() and clone() are protected in Object class" New topic
Author

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

Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
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?.




The value of an idea lies in the usage of it.
Gunjan Sahay
Greenhorn

Joined: Aug 07, 2002
Posts: 11
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
So, why is clone() in Object at all? Why is it not in Cloneable?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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

Joined: Aug 07, 2004
Posts: 5
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.


<a href="http://www.blogeasy.com/" target="_blank" rel="nofollow">Free Blog</a> | <a href="http://www.blogeasy.com/" target="_blank" rel="nofollow">Free Blog Site</a>
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
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 ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why finalize() and clone() are protected in Object class
 
Similar Threads
about core java
protected methods of java.lang.Object
about core java
clone() and finalize()
why only clone method and finalize method in object class are protected?