• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

why only clone method and finalize method in object class are protected?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why only the clone and finalize methods in the object class are protected what is reson behind it.....? why not other methods are made protected
 
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize method is protected as you are supposed to provide cleanup code in it. If suppose your class has an open IO connection which is closed in the finalize method. If the finalize method is not protected, then someone can call finalize on object of your class which will close the connection. Then any IO operation done on that object will throw IOException. To prevent this the finalize method has been kept protected.

a class must provide implementation for the clone method itself. If a class (take A as the class name)doesn't provide implementation for the clone method then classes creating objects of A class must not be able to call clone on the objects of A. This is why clone in Object class is protected as you cannot call protected methods on an object of a class.

Now you must be asking why??? this is because the clone method of Object class cannot create clone of an object on its own. You need to deep copy the fields in your class that are references.

eg
class X
{
//some declarations....
public X clone()
{
//clone fields
}
}
class A
{
X obj;
A(){}
A(X obj){this.obj = obj;}
public A clone()
{
new A(obj.clone());
}
}

If you relied on the clone method of the Object class then the obj would not be cloned...

Please correct me if I have done something terribly wrong......
 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am bit confused...please need a bit more explaination...
 
Ankit Garg
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets assume that the finalize method in Object class is public.. the following class uses an IO connection and closes the connection in the finalize method.



Now the following class does this

So the object stored in reference iTest is becomes wasted when you call the finalize method...that's why the finalize method in Object class is protected....Your class can then volunteer to make the finalize method public but that's not needed in practice....

Please tell me if you get this...then I will try to explain the equals method....
 
reply
    Bookmark Topic Watch Topic
  • New Topic