Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:29:33+00:00 2026-06-09T14:29:33+00:00

I have a quick question regarding the clone() method in Java, used as super.clone()

  • 0

I have a quick question regarding the clone() method in Java, used as super.clone() in regard to inheritance – where I call the clone() method in the parent class all the way up from the button.

The clone() method is supposed to return a copy of this object, however if I have three classes in an inheritance heirachy and call super.clone() three times, why doesn’t the highest class in the inheritance heirachy, just under class Object, get a copy of that class returned?

Suppose we have three classes: A, B and C, where A -> B -> C (inherit = ->)

Then calling super.clone() in class C, invokes clone() in B which calls super.clone(), invoke clone() in A which call super.clone() ‘this time Object.clone() gets called’. Why is it not a copy of the this object with respect to class A that gets returned from Object.clone()? That sounds logical to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:29:35+00:00Added an answer on June 9, 2026 at 2:29 pm

    It sounds like there are at least two problems at work here:

    1. It sounds like you’re confused about how clone() normally gets implemented.

    2. It sounds like you’re thinking that cloning is a good idea (vs. using a copy constructor, factories or their equivalent).

    Here is an example of an implementation of a clone method:

    @Override 
    public Object clone() throws CloneNotSupportedException {   
        //get initial bit-by-bit copy, which handles all immutable fields
        Fruit result = (Fruit)super.clone();
    
        //mutable fields need to be made independent of this object, for reasons
        //similar to those for defensive copies - to prevent unwanted access to
        //this object's internal state
        result.fBestBeforeDate = new Date( this.fBestBeforeDate.getTime() );
    
        return result;
    }
    

    Note that the result of super.clone() is immediately cast to a Fruit. That allows the inheriting method to then modify the Fruit-specific member data (fBestBeforeDate in this case).

    Thus, the call to a child clone() method, while it will call the parents’ clones, also adds its own specific modifications to the newly made copy. What comes out, in this case, will be a Fruit, not an Object.

    Now, more importantly, cloning is a bad idea. Copy constructors and factories provide much more intuitive and easily maintained alternatives. Try reading the header on the Java Practices link that I attached to the example: that summarizes some of the problems. Josh Bloch also has a much longer discussion: cloning should definitely be avoided. Here is an excellent summary paragraph on why he thinks cloning is a problem:

    Object’s clone method is very tricky. It’s based on field copies, and
    it’s “extra-linguistic.” It creates an object without calling a
    constructor. There are no guarantees that it preserves the invariants
    established by the constructors. There have been lots of bugs over the
    years, both in and outside Sun, stemming from the fact that if you
    just call super.clone repeatedly up the chain until you have cloned an
    object, you have a shallow copy of the object. The clone generally
    shares state with the object being cloned. If that state is mutable,
    you don’t have two independent objects. If you modify one, the other
    changes as well. And all of a sudden, you get random behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quick question regarding the proper way to access Javascript class member
this is my very first post! I have a quick question in regarding inheritance
I have a quick question regarding debug and release in VS 2008. I have
Hi I have a quick question regarding json responses from the web services, I
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
Good afternoon, I have a quick 'top level' question regarding the usage of Facebook
I have a quick question regarding the creation of absolute URLs within a symfony
I have a quick question regarding memory management that I am not quite sure
I have a quick question regarding a database that I am designing and making
I have this quick question regarding cost estimation using Function Points. We are doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.