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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:46:39+00:00 2026-06-16T16:46:39+00:00

Assume I have a class A , and B which derives from A :

  • 0

Assume I have a class A, and B which derives from A:

class A : ICloneable
{
    public object Clone() {...}
}

class B : A, ICloneable
{
    public object Clone() {...}
}

which gives

'B.Clone()' hides inherited member 'A.Clone()'. Use the new keyword if hiding was intended.

warning.

(1) What is the suggested way? using new or declaring A.Clone() as virtual and override in B?

(2) If there are some members in A and properly cloned in A.Clone(), is there an easy way to clone them in B.Clone() or do I have to explicitly clone them in B.Clone() also?

  • 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-16T16:46:40+00:00Added an answer on June 16, 2026 at 4:46 pm

    If you have access to your source (which I’m guessing is the case here) then absolutely declare it as virtual and override it. If hide the base Clone with new might be a bad idea. If any code doesn’t know that it’s working with a B, then it will fire the wrong clone method and not return a proper clone.

    Regarding the assignment of properties, perhaps consider implementing copy constructors and each level can handle its own cloning:

        public class A : ICloneable
        {
            public int PropertyA { get; private set; }
    
            public A()
            {
    
            }
    
            protected A(A copy)
            {
                this.PropertyA = copy.PropertyA;
            }
    
            public virtual object Clone()
            {
                return new A(this);
            }
        }
    
        public class B : A, ICloneable
        {
            public int PropertyB { get; private set; }
    
            public B()
            {
    
            }
    
            protected B(B copy)
                : base(copy)
            {
                this.PropertyB = this.PropertyB;
            }
    
            public override object Clone()
            {
                return new B(this);
            }
        }
    

    Each copy constructor calls the base copy constructor passing itself down the chain. Each inheritance level copies the properties belonging to it directly.

    EDIT: If you use the new keyword to hide the base implementation, here’s an example of what might happen. With a sample implementation (which on the face of it looks fine)

    public class A : ICloneable
    {
        public int PropertyA { get; protected set; }
    
        public object Clone()
        {
            Console.WriteLine("Clone A called");
            A copy = new A();
            copy.PropertyA = this.PropertyA;
            return copy;
        }
    }
    
    public class B : A, ICloneable
    {
        public int PropertyB { get; protected set; }
    
        public new object Clone()
        {
            Console.WriteLine("Clone B called");
            B copy = new B();
            copy.PropertyA = this.PropertyA;
            copy.PropertyB = this.PropertyB;
            return copy;
        }
    }
    

    But when you use it:

    B b = new B();
    A a = b;
    B bCopy = (B)a.Clone();
    //"Clone A called" Throws InvalidCastException! We have an A!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume the following: we have class B, which is a private class nested inside
Assume I have a class that looks like this: class Sample { public string
Assume I have a class class A { char *attr1,*attr2; public: . . .
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume you have a list item, <li id=foo> which you want to fade from
So assume I have one class named Program which contains my whole program of
I have a templated class which looks as follows (assume a using namespace std
Lets assume we have a class which will be widely used throughout the (c#)
I have a class which I need to use to extend different classes (up
I have public class Entity On which works class: public class Table<Target extends Entity>

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.