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 8755223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:48:32+00:00 2026-06-13T13:48:32+00:00

This is raised because of the technical difficulties faced in my Project. Problem: I

  • 0

This is raised because of the technical difficulties faced in my Project.

Problem:
I need to clone a Object of a Class where it extended the properties(Inheritance) from a third party library class(where we don’t have access to modify its contents)

Let me explain with example below:

Parent Class:

public class UnChangeableBaseClass {

  //fields and Methods

}

Child Class:

class DerivedLocalClass extends UnChangeableBaseClass implements Cloneable {

   // local fields and methods


   public Object clone(){

      Object clonedObj= null;

      try{
       clonedObj = super.clone();
      }
      catch(CloneNotSupportedException e){
        //log exceptions
      }

   }

}

When I try to do this, super.clone() method refers to Class – UnChangeableBaseClass Type and it doesn’t overrides the Object clone() methods. I believe all classes were extended with java.lang.Object class, implicitly protected Object clone() method would be inherited to this Parent Class. So, i thought in such a way that this method in Derived Class would overrides the Parent/Object clone method. But during runtime JVM search for the clone method explicitly defined in UnChangeableBaseClass. Hope I explained in proper way without confusing you.

My questions are follows:

  1. How can I implement clone method in this typical case, where we cannot add any method
    in parent class to have super.clone() to call Object clone method.

  2. If above case is not possible, is there any other way to clone the Derived Class
    Object (by considering all the limitations in above scenario)

  3. Finally, just to know the reason for this JVM behaviour (described above).

  • 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-13T13:48:33+00:00Added an answer on June 13, 2026 at 1:48 pm

    Correct Method signature is below

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    

    Clone is Protected method in Object class so it is accessible to you inside class, and if you are extending from it.
    super.clone() is just needed to call clone() method from object which calls method internalClone on this which is current class object.

       internalClone((Cloneable) this);
    

    So above clone() method inside Object will only throw CloneNotSupportedException if instance on which it is called is not Cloneable

    I see some misconceptions about clone method

    1. clone() method is protected inside Object class so you can not call clone() outside of class. e.g. child.clone() unless you override it and make access public
    2. Cloneable is marker interface and if you do not mark class Cloneable then you will get CloneNotSupportedException if you call clone() method
    3. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
    4. By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

    So below code works fine

    public  class Child extends UnChangeableBaseClass
            implements
                Cloneable {
    
        int index = 0;
    
        public Child(int index) {
    
            this.index = 10;
        }
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    }
    

    References :

    1. Object#clone()
    2. Cloneable
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of my friends raised this problem to me. I was stuck because I
I've seen that a few instances of this problem have been raised already. However,
I raised this question up because I have seen Windows 7 64 bits able
A coworker's typo when calling a subref raised this strange syntax question. If I
This question is an extension to the one raised here: Using factory_girl in Rails
I ask this question following the issue I raised here . The point is
This question follows on from a previous question, that has raised a further issue.
In this example, an error is raised if either row.FirstName or row.LastName are NULL
See the title of this question. I want to play with the exception raised
Is this even possible? For example, let's say I have the following: class Window

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.