• 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

Why protected method in class Object?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why class Object have two PROTECTED method -- clone() and finalize(). And in JUnit's source code, I notice that kent write :
public class AClass extends Object {}
I really don't understand what diffrient from
public class AClass {}
Any help? Thanks!
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Why is clone protected?

Nobody is going to claim that the design of the cloning mechanism in Java is
perfect (Cloneable? Is that even the correct spelling ), but not all objects
are designed to be cloneable --if method clone were public, then you could apply
it to objects that you didn't intend to:

If you are making a class cloneable, you have the option of changing the access
level of clone to public when you override the method. You also have the option
of losing the "thow CloneNoSupportedException", if that's your design:

So clone is protected because it's up to subclasses to set up the cloning mechanism.


2. Why is finalize protected?

You should never call finalize, other that having a subclass's finalize method
call its superclass's finalize, where protected access works just fine. The garbage collector
may call the finalize method, but it's able to get around the proctected access. Thus
public access is not needed. And as an aside, there are good reasons to avoid overriding
finalize in most cases, anyway.

3. "class AClass extends Object" versus "class AClass"

There is no difference. Some people write "extends Object" as a matter of coding style,
some don't, for the same reason! Take your pick and be consistent.
[ November 01, 2005: Message edited by: Jeff Albrechtsen ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize() is protected because only the JVM should access it, and it shouldn't be called from any user code. If it's public you could finalize an Object while it is not yet eligible for garbage collection. It is not package visible or private because subclasses should always call super.finalize(). Therefore, only protected will do.

clone() is protected for similar reasons. You cannot just call clone() from other code - if the class is not Cloneable it will go wrong. However, you need access to super.clone() from any subclass. If clone() is supported in a class it will be made public by that class.

I only have one problem with clone() - I think Cloneable should require any interface implementing Cloneable to implement clone() as well. What's the reason for implementing Cloneable if you don't provide a public void clone() anyway? Unless you're using it to clone subclasses only, and want to be able to call super.clone().
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Isaac,

Welcome to JavaRanch!

First, the easy question: there's absolutely no difference between saying that a class "extends Object" or having no extends clause at all. Some people like to include "extends Object" for clarity, and some people include it because they're using an IDE that inserts it automatically. But most people don't use it, and most people don't like it.

Now, the other question: both clone() and finalize() are strange methods, each with a special story. finalize() is intended to be called by the JVM on an object that's about to be garbage collected; no ordinary code should ever call it, except that if you're overriding finalize() (you can do this to add some clean-up code that is called before garbage collection) then you should always call super.finalize(). That's why finalize() is "protected" -- so that subclasses of Object can override it and call it. Otherwise, it could be private. There's no need for it to be public, as no one should ever call it otherwise.

Finally, clone() (which makes a copy of the object) is designed so that a class can prevent anyone from calling it unless the author of that class wants them to. Accordingly, clone() is protected. If the author of a class wants to make clone() publically available, then the author should override clone(), call super.clone(), and make that override public. To make clone() work, by the way, the author also has to declare the class implements the Cloneable interface -- otherwise Object.clone() will refuse to work.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as I was posting a clone method, I should add that in the current
version of Java (1.5), they've adding a contraviance (or is that covariance)
rule, so when you override a method, you can "subtype" the return type:

You can see this is already in effect for some types, like array types:
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I will claim the cloning facility of Java is perfect.

Please stop making the clone method public. its a facility to provide a class with a bottom up copy of itself. Nothing more nothing less.

If you want to provide to the _public_ a way to get a copy of your object try creating your own method


Again, do not publicize the clone method, use it internally. Thats what I prefer and it removes the confusion people have. It really does not matter so long as you recognize that Cloneable is a facility NOT a simple interface.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mr. C Lamont Gilbert:

If you want to provide to the _public_ a way to get a copy of your object try creating your own method



That's fine, too. I see this as a to-mah-to/to-may-to issue. You introduce
getCopy/I reintroduce clone. All I can say is that in the API, they tend
to use clone -- for example, cloning arrays, concrete collections like
ArrayList, Calendar and so on. So there is a pattern if you wish to follow it.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So CLG, have you noticed that the API for Cloneable specifically recommends making the clone() method public? Doesn't seem "perfect" if the documentation is contrary to what you assert is best practice, does it? The whole thing (implementation and documentation) just seems needlessly convoluted to me. I know how to using cloning in Java correctly; I just wish they'd set it up in a more straightforward manner.
[ November 01, 2005: Message edited by: Jim Yingst ]
 
Isaac Han
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all of you.

But, i still puzzle: Object class is the root of ALL classes, So i conclude that there are no difference between "public" and "protectd" in Object class.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that I know much about clone() . . . .

The difference between public and protected is that protected allows for access from any class in the same package (well, you can't write a new java.lang.Anything class) or from inside a subclass.
So every class can have the statement in, and your class MyClass can have a method like
but the point of being protected means you can't call the super.clone() method from another class in your project.
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Han wrote:But, i still puzzle


To add to the last post (10 years later )

If a Cat class overrides the clone() method in the Animal class, a Dog class cannot access the clone() method in the Cat class because it is protected within Cat and its subclasses i.e. only subclasses of Cat can access it. The reason for this is that if a Cat decides to clone itself via a Cat cloning strategy, a Dog should not be allowed to access the Cat cloning strategy and clone itself like a Cat; only Cat and subclasses of Cat can do that. A Dog class can however, access the clone() method in the Animal class and clone itself like an Animal or override the clone() method in the Animal class and clone itself like a Dog.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not convinced, I am afraid.
If a Cat object can be cloned then it should be possible for that to happen outside its class. You cannot give clone() private or package‑private (=default) access because that would breach all the rules of overriding. So the clone method is accessible to all classes in the same package even if it is protected; a Cat object probably therefore can get a clone of a Dog object.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If a Cat object can be cloned then it should be possible for that to happen outside its class.


I agree. A Cat object can be cloned outside the Cat class, however, only an instance of Cat or its subclass can access the clone() method overridden in the Cat class (I'm assuming that the Cat class overrides the clone() method in the Animal class).

So the clone method is accessible to all classes in the same package even if it is protected


I agree. Any non-private method within a package is accessible to any class within the same package even if it is protected, however, the value of the protected access modifier comes into play when classes are located in different packages.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic