• 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

Object Cloning

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Sheriff
    Posts: 9707
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    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

    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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 9707
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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...
     
    reply
      Bookmark Topic Watch Topic
    • New Topic