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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:19:00+00:00 2026-05-18T08:19:00+00:00

So I have the following classes: class A{ public A(int n1){ n=n1; } int

  • 0

So I have the following classes:

class A{
    public A(int n1){
        n=n1;
    }
    int n;
}

class B extends A{
    public B(int n2){
        super(n2);
        cnt=1;
    }
    int cnt;
}
class C extends B{
    public C(int n3){
        super(n3);
        clr="red";
    }
    String clr;
}

public class Driver {
    public static void main(String[] args){
        A a,b,c,d,e;
        a=new B(200); d=a.copy();
        b=new C(100); e=b.copy();
    }
}

I am asked to define the method copy() in classes A,B,C. The copy method essentially makes a copy of all nested objects.

I have 2 questions:

  1. I don’t see any nested objects being constructed, why does he ask me to make a copy of all nested objects? Is it because when I construct a subclass object, a base class object is constructed and nests inside the subclass object?

  2. Is it correct to write the method as follows (take class B for example):

class B extends A{
    public B(int n2){
        super(n2);
        cnt=1;
    }
    int cnt;
    public A copy(){
        A copy_ref=new B(1);
        ((B)copy_ref).cnt=this.cnt;
        copy_ref.n=super.n;
        return copy_ref;
    }
}
  • 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-05-18T08:19:01+00:00Added an answer on May 18, 2026 at 8:19 am

    I think you are confusing to different concepts.

    You are confusing the has-a relationship with the is-a relationship.

    In your code C is a B and also an A: C has an is-a relationship with B and A.
    C does not contain an instance of B or A (that would be an has-a relationship).

    Since C is an B and A, it contains all the members of B and A. Calling a copy of C will copy all of its members variables. You do not need to create any particular method, you can just use the already defined Object.clone method.

    If you want to define your own clone/copy method I suggest you look at the following article on the subject.

    Enjoy !

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

Sidebar

Related Questions

I have the following classes class A{ private String name; private int value; public
I have the following classes: public class Person { public String FirstName { set;
I have the following two classes: public class Address { public string AddressLine1 {
I have following classes. class A { public: void fun(); } class B: public
We have following classes public class MyPropertyBase { public int StartOffset { get; set;
I have the following classes: class Person { public string Name { get; set;
I have the following simple textbook classes defined: public class Product { public int
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I have the following classes: public class Entity<T> where T : Entity<T> { public

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.