This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Object Cloning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object Cloning" Watch "Object Cloning" New topic
Author

Object Cloning

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Hi All,

The clone method is declared as protected in Object class. Then why can't our code simply call anObject.clone() ?

Aren't protected methods accessible from any subclass, and isn't every class a subclass of Object. So why above ?

A subclass can call a protected clone method only to clone its own objects. Why ?

You must redefine clone to be public to allow objects to be cloned by any methods. Why?


Please do clarify my doubts.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
Can't remember the full details offhand, but you
  • implement the Cloneable interface
  • Override the clone() method, probably with public access
  • Call Foo fff = (Foo)super.clone(); in the clone method
  • There are other bits I have forgotten.
  • So you end up with the superclass' clone method being protected, but you can expose it as a public method.

    Note that in Effective Java (you might be able to find a sample chapter with clone() in), Joshua Bloch says the cloning mechanism was poorly designed.
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9280
        
      17

    The clone method in Object class was set protected so that other classes are not able to access it on an instance of a class. You first need to clear your mind about protected access.




    Here both B and C classes can access the val variable, but only on an instance of their own classes. So if you try this



    Coming back to clone method, suppose you create a class like this



    Now only the Employee class can clone its instances. If clone was declared public in Object class, then other classes would've been able to clone instances of Employee class. But there won't be any deep cloning i.e. if you clone an instance of Employee class, then the original object and the clone Employee objects would use the same Address object. That's why it is made protected. If you want other classes to be able to clone instances of your class, you'll override the clone method, make it public and take care of deep cloning...


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Siddharth Bhargava
    Ranch Hand

    Joined: Feb 23, 2007
    Posts: 248
    I am Sorry my concepts are still not crystal clear.

    Please explain in more detail and with a simpler example.

    Thanks
    G.Sathish kumar
    Ranch Hand

    Joined: Jul 27, 2009
    Posts: 84
    Siddharth Bhargava wrote:I am Sorry my concepts are still not crystal clear.

    Please explain in more detail and with a simpler example.

    Thanks


    CHECK THE BELOW LINK

    YOU CAN UNDERSTAND DEEPLY

    http://javainnovations.blogspot.com/2008/06/cloning-of-java-objects.html


    Thanks
    Sathish kumar
    SCJP, SCWCD
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19538
        
      16

    G.Sathish kumar wrote:
    Siddharth Bhargava wrote:I am Sorry my concepts are still not crystal clear.

    Please explain in more detail and with a simpler example.

    Thanks


    CHECK THE BELOW LINK

    YOU CAN UNDERSTAND DEEPLY

    http://javainnovations.blogspot.com/2008/06/cloning-of-java-objects.html

    Please keep it down.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Siddharth Bhargava
    Ranch Hand

    Joined: Feb 23, 2007
    Posts: 248
    Hi Sathish,

    I read the whole article given in the URL. The article is good in clearing the basics of Cloning in Java.

    But the article still didn't clear my concepts and questions which still remain.

    - The clone method is declared as protected in Object class. Then why can't our code simply call anObject.clone() ?

    - Aren't protected methods accessible from any subclass, and isn't every class a subclass of Object. So why above ?

    - A subclass can call a protected clone method only to clone its own objects. Why ?

    - You must redefine clone to be public to allow objects to be cloned by any methods. Why?

    Thanks and Regards,
    Siddharth Bhargava.
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9280
        
      17

    Siddharth all of your questions are now related to access to a protected member. Go through an article like this or this...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Object Cloning
     
    Similar Threads
    Public Vs Protected methods in object class
    about core java
    inheritance question
    About the clone method
    why only clone method and finalize method in object class are protected?