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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:52:10+00:00 2026-06-10T13:52:10+00:00

I have two classes: the parent class A and some subclass B, which extends

  • 0

I have two classes: the parent class A and some subclass B, which extends A and has several more fields than A. I want to be able to clone both of these classes, so I have overridden the clone() method from the Object class.

Since class B is basically class A with some extra fields I would like to use the clone of class A in the clone() implementation of class B. I have tried the something along the lines of:

    public class A
    {
      public Object clone()
      {
        A cloneA = new A();
        cloneA.foo = this.foo;
        cloneA.bar = this.bar;
        return cloneA;
      }
    }

    public class B extends B
    {
      public Object clone()
      {
        Object cloneA = super.clone();
        B cloneB = (B)cloneA;
        cloneB.fieldSpecificForB = this.fieldSpecificForB;
        return cloneB;
      }
    }

This way I would not have to duplicate all the clone logic from A’s clone() method in B’s clone() method. Unfortanely this does not work since Java does not allow an object of class A to be casted to an object of class B. I searched around for an answer on how to do this, but it seems there is no way to reuse the cloning logic from A. Does this mean that every time I add another field to A, I have to manually add the copying of this field to the clone() method of all the subclasses. This seems to be very error-prone…

  • 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-10T13:52:12+00:00Added an answer on June 10, 2026 at 1:52 pm

    The way clone works is, is that it copies all fields for you automatically. You must however call super.clone() for it to work:

    public class A implements Cloneable  // add 'implements'
    {
      public Object clone()
      {
        A cloneA = super.clone(); // this copies all fields and returns something of *the same type* as 'this'...
        return cloneA;
      }
    }
    
    public class B extends A //supposed to extend 'A' right?
    {
      public Object clone()
      {
        Object cloneA = super.clone(); // this returns object of runtime type B
        B cloneB = (B)cloneA; // will work
        return cloneB;
      }
    }
    

    Edit: Actually your clone methods do nothing now, so it is equivalent to write:

    public class A implements Cloneable  //add 'implements'
    {
    
    }
    
    public class B extends A //supposed to extend 'A' right?
    {
    
    }
    

    The implements Cloneable part is the one that does the trick. See also Cloneable.

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

Sidebar

Related Questions

I have a MustInherit Parent class with two Child classes which Inherit from the
I have two classes with a parent-child relationship (the Parent class has-a Child class),
I basically have two child classes which have the same parent class. what I
i have made two classes, which is person - the parent class and a
Supposed I have two classes like this: public class Parent { public string Id
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes Foo and Bar that Bar extends Foo as below: class
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have two closely related classes which I'll call Widget and Sprocket. Sprocket has
Suppose I have two classes (one a parent and one a subclass). How do

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.