title: Overriding fields or properties in subclasses
body: <p>I have an abstract base class and I want to declare a field or a property that will have a different value in each class that inherits from this parent class. </p>
  
  <p>I want to define it in the baseclass so I can reference it in a base class method - for example overriding ToString to say "This object is of type <i>property/field</i>".
  I have got three ways that I can see of doing this, but I was wondering - what is the best or accepted way of doing this? Newbie question, sorry.</p>
  
  <p><strong>Option 1:</strong><br>
  Use an abstract Property and override it on the inherited classes. This benefits from being enforced (you have to override it) and it is clean. But, it feels slightly wrong to return a hard-code value rather than encapsulate a field and it is a few lines of code instead of just. I also have to declare a body for "set" but that is less important (and there is probably a way to avoid that which I am not aware of). </p>
  
  <pre><code>abstract class Father
  {
      abstract public int MyInt { get; set;}
  }
  
  class Son : Father
  {
      public override int MyInt
      {
          get { return 1; }
          set { }
      }
  }
  </code></pre>
  
  <p><strong>Option 2</strong><br>
  I can declare a public field (or a protected field) and explicitly override it in the inherited class. The example below will give me a warning to use "new" and I can probably do that, but it feels wrong and it breaks the polymorphism, which was the whole point. Doesn't seem like a good idea...</p>
  
  <pre><code>abstract class Mother
  {
      public int MyInt = 0;
  }
  
  class Daughter : Mother
  {
      public int MyInt = 1;
  }
  </code></pre>
  
  <p><strong>Option 3</strong><br>
  I can use a protected field and set the value in the constructor. This seems pretty tidy but relies on me ensuring the constructor always sets this and with multiple overloaded constructors there is always a chance some code path won't set the value.</p>
  
  <pre><code>abstract class Aunt
  {
      protected int MyInt;
  }
  
  class Niece : Aunt
  {
      public Niece()
      {
          MyInt = 1;
      }
  }
  </code></pre>
  
  <p>It's a bit of a theoretical question and I guess the answer has to be option 1 as it is the only <i>safe</i> option but I am just getting to grips with C# and wanted to ask this of people with more experience.</p>