This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
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"
Preparing for OCPJWCD 6.
Joined: Oct 30, 2001
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.
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.
Joined: Aug 21, 2007
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?