• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Object class

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beginner question, not Advanced forum material.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
reply
    Bookmark Topic Watch Topic
  • New Topic