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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:15:30+00:00 2026-05-14T03:15:30+00:00

I was wondering how to do this, consider the following classes public class Fruit

  • 0

I was wondering how to do this, consider the following classes

public class Fruit
{
    public string Name { get; set; }
    public Color Color { get; set; }
}
public class Apple : Fruit
{
    public Apple()
    {

    }
}

How can I instantiate a new fruit but upcast to Apple, is there a way to instantiate a bunch of Fruit and make them apples with the name & color set. Do I need to manually deep copy?

Of course this fails

Fruit a = new Fruit();
a.Name = "FirstApple";
a.Color = Color.Red;
Apple wa = a as Apple;
System.Diagnostics.Debug.Print("Apple name: " + wa.Name);

Do I need to pass in a Fruit to the AppleCTor and manually set the name and color( or 1-n properties) Is there an better design to do this?

  • 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-14T03:15:30+00:00Added an answer on May 14, 2026 at 3:15 am

    In your model, a Fruit is not an Apple – if you want an Apple, instantiate one. Alternatively, if you want to create an Apple using a Fruit as a starting point, create a constructor for Apple that can accept a Fruit.

    You should note, that object models where intermediate classes in the inheritance hierarchy are instantiable (not abstract) is often a bad idea. In some cases, such hierarchies may be appropriate – but I’ve found that in general they are more trouble than they are worth. Think really hard about whether both Fruit and Apple need to be concrete and instantiable.

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

Sidebar

Related Questions

consider the following basic class layout: public class Base : IComparable<Base> { public int
Consider this HTML: <form id=MyForm> <input name=Input1/> </form> To get the form, I would
Consider the following example: public class Sandbox { public interface Listener<T extends JComponent> {
Consider following example: class CBase abstract { protected: CBase() { } }; I could
Consider the following simple html page markup: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
Consider the following code: float validateEntry() { string entry; float value; getline(cin, entry); value
Consider the following code? I was wondering, if I change it from (Function body
I can't seem to find an answer to this problem, and I'm wondering if
I'm using the MySQL .NET Connector, and I'm wondering how I can get the
I've been wondering this for a while but since it hasn't come up much

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.