6

I have a class that has private fields... (cars)

I then inherit from this class... (Audi)

In the (Audi) class, when I type this. in the constructor...

the private fields are not available...

Do I need to do anything special to expose this private fields in (cars) class so that they are accessible via this. in (Audi class)?

4 Answers 4

20

One (bad) option is to make the fields protected - but don't do this; it still breaks proper encapsulation. Two good options:

  • make the setter protected
  • provide a constructor that accepts the values

examples:

public string Name { get; protected set; }

(C# 2.0)

private string name;
public string Name {
    get { return name; }
    protected set { name = value; }
}

or:

class BaseType {
  private string name;
  public BaseType(string name) {
    this.name = name;
  }
}
class DerivedType : BaseType {
  public DerivedType() : base("Foo") {}
}
Sign up to request clarification or add additional context in comments.

Comments

17

Philippe's suggestion to declare the fields as protected instead of private will indeed work - but I suggest you don't do it anyway.

Why should a derived class care about an implementation detail of how the data is stored? I suggest you expose protected properties which are (currently) backed by those fields, instead of exposing the fields themselves.

I treat the API you expose to derived classes as very similar to the API you expose to other types - it should be a higher level of abstraction than implementation details which you may want to change later.

Comments

11

You should declare them as "protected" instead of private

8 Comments

IMO, making fields protected is a bad idea.
Same for me, I prefer private fields and protected properties. But it's not always worth on really small projects/cases
Whether it's a good idea or not, it's the answer to the question. I guess providing a correct answer deserves several downvotes.
Right, I didn't know about that. At my company where use c# 2.0. So yes, forget about protected fields. Fields are ALWAYS private. Don't let anyone touch your member, except your friends ( hehehe )
Phillipe, the purpose of votes is to sort the answers, the criterion is 'unhelpful'. So yes, -1 from me.
|
5

You are probably looking for a concept called constructor inheritance. You can forward arguments to the base classes constructor - see this example, where the Audi has a flag indicating whether it's an S-Line edition or not:

namespace ConstructorInheritance
{
    abstract class Car
    {
        private int horsePower;
        private int maximumSpeed;

        public Car(int horsePower, int maximumSpeed)
        {
            this.horsePower = horsePower;
            this.maximumSpeed = maximumSpeed;
        }
    }

    class Audi : Car
    {
        private bool isSLineEdition = false;

        // note, how the base constructor is called _and_ the S-Line variable is set in Audi's constructor!
        public Audi(bool isSLineEdition, int horsePower, int maximumSpeed)
            : base(horsePower, maximumSpeed)
        {
            this.isSLineEdition = isSLineEdition;
        }
    }

    class Program
{
    static void Main(string[] args)
    {
        Car car = new Audi(true, 210, 255);
        // break here and watch the car instance in the debugger...
    }
}    }

3 Comments

+1 for complementary informations that I think OP will find useful
Hmm... it's not really clear that the OP needs to do anything within the constructor. No downvote as there's no inaccurate information and it might help the OP, but it doesn't really address the full question.
How could he ask questions about things he does not know? IMHO it is valid to see the underlying problem rather than just answering questions on how to encapsulate variables or the difference between protected and private access modifiers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.