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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:25:07+00:00 2026-06-04T20:25:07+00:00

Duplicate of C# Accessing a subclass property – see additional solution approaches there. I

  • 0

Duplicate of C# Accessing a subclass property – see additional solution approaches there.

I have an object model where each object has a parent and I would like to access the parent property with the specific type for each object. So e.g. Wheel shall have a parent property of type Car. My guess is that there is an established design pattern for achieving this (in .NET). However, I have not found it so the design I came up with looks like this

public class BaseObject<T> where T : BaseObject<T>
{
    private T mParent;
    protected T Parent
    {
        get { return mParent; }
    }

    public BaseObject(T parent)
    {
        mParent = parent;
    }

    public void Save() ...
    public void Delete() ...
    public BaseObject<T> Clone() ...
}

public class Car : BaseObject<Car>
{
    public Car(Car parent) : base(parent)
    {
    }

    public bool Is4WD()
    {
        return true;
    }
}

public class Wheel : BaseObject<Car>
{
    public Wheel(Car parent) : base(parent)
    {
    }

    public float RequiredTirePressure()
    {
        return Parent.Is4WD() ? 3.0f : 2.5f;
    }
}

Do you see any design or performance drawbacks with this approach? Can you recommend a better design?

  • 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-04T20:25:09+00:00Added an answer on June 4, 2026 at 8:25 pm

    The answer is to use composition instead of inheritance. A wheel is not a car, so it should not extend from Car. Instead, have the Wheel take a Car in its constructor.

    For example:

    public class Wheel {
        private Car car;
    
        public Wheel(Car car) {
            this.car = car;
        }
    
        public float RequiredTirePressure() {
            return car.Is4WD() ? 3.0f : 2.5f;
        }
    }
    
    • Composition over inheritance
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Accessing Class Properties with Spaces i have and object file and i
Possible Duplicate: Accessing numerical property in a json object Using the facebook graph api,
Possible Duplicate: Accessing Windows registry with PHP and DOTNET class Is there a way
Possible Duplicate: Accessing Password Protected Network Drives in Windows in C#? I have ComputerA
Possible Duplicate: obj-c access property dynamically I'm attempting to access an object’s properties dynamically
Possible Duplicate: How do I access this object property? The data returned in a
Possible Duplicate: Accessing inherited variable from templated parent class I have been implementing a
Possible Duplicate: Accessing inherited variable from templated parent class There's this class: template<typename T>
Possible Duplicate: difference between accessing a property via “propertyname” versus “self.propertyname” in objective-c? In
Duplicate: Django or Ruby-On-Rails? I have been reading on Ruby on Rails, and it

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.