Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Cloning

 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 9528
    33
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Siddharth Bhargava
    Ranch Hand
    Posts: 280
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20665
    65
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Siddharth Bhargava
    Ranch Hand
    Posts: 280
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 9528
    33
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Siddharth all of your questions are now related to access to a protected member. Go through an article like this or this...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic