• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Object class

 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why object class has protected method(clone,finalize) ? As we know that every class is child of Object class then does it make sense?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beginner question, not Advanced forum material.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. The designers of the Object class probably made these methods protected to give you a hint that you should never call these methods directly - even though it doesn't really make it impossible to do so.

And welcome to JavaRanch!
[ August 21, 2007: Message edited by: Jesper Young ]
 
Saurabh Patil
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
Good question. The designers of the Object class probably made these methods protected to give you a hint that you should never call these methods directly - even though it doesn't really make it impossible to do so.

And welcome to JavaRanch!

[ August 21, 2007: Message edited by: Jesper Young ]



Can you please explain "call these method directly"!! :confused"
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try calling the protected clone() method of class A from class B, where A and B are not superclass and subclass. You can't.

If you need to be able to do that sort of thing, you must override the protected clone() method with a public one.

But trying to learn much from the clone feature of Java is a mistake, because it was one of the first features added to the language and is very badly designed, by today's standards.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saurabh Patil:
Can you please explain "call these method directly"!!

What I mean with that is that you should not write a Java program that calls the finalize() method of any object. This is a special method that the JVM calls when necessary - when the garbage collection process is cleaning up the object.

For the clone() method, what I said is actually not true. If you want objects of your class to be cloneable, you should make your class implement interface Cloneable and provide a public clone() method in your class, according to the documentation of interface Cloneable:
By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton for answers.

But still my questation is "Why in Object class ".As its a Father of all the born java classes?

Although,i do agree that protected method is for overriding purposes.No doubt.if you have public method you can also override these methods in your child class?

So ,these "protected" serve any specfic means ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic