-3
public abstract class SampleA
{
    public abstract string PropertyA {get;set;}
}

How to Override abstract PropertyA ?

enter image description here

LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
Nagaraj
  • 45
  • 4
  • have you checked this: https://stackoverflow.com/questions/8905432/what-is-the-use-of-abstract-override-in-c – Maytham Fahmi Apr 26 '19 at 06:17
  • Can you explain what are you exactly confused about? Can't you just subclass it and declare a property with the same name with the `override` modifier? – Sweeper Apr 26 '19 at 06:17
  • @Sweeper I tried overriding like below ```public class Sample : SampleA { public override string PropertyA = "Override"; }``` but I am getting issue saying I can't override and I need to use new key word – Nagaraj Apr 26 '19 at 06:23
  • Possible duplicate of [Inheriting abstract classes with abstract properties](https://stackoverflow.com/questions/16597653/inheriting-abstract-classes-with-abstract-properties) – Raghavendra Apr 26 '19 at 06:23

2 Answers2

1

You are trying to override a property with a field

Properties provide access to a field by get and set value accessors. Please read this to better understand the difference. So because they are not the same, your IDE proposed you to hide the parent Property by using the new-Keyword.

If you want to know why the new Keyword didn't work in your case, read this.

Improve your Design

In your Question, it seems like PropertyA in your code needs to be set on inherited classes, but can't be changed from outside. So maybe do it like this:

public abstract class SampleA
{
    // no setter -> cant be changed after initialization
    public abstract string PropertyA { get; } 

    // protected setter -> can only be changed from inside SampleA or Sample
    public abstract string PropertyB { get; protected set; } 
}

public class Sample : SampleA
{
    public override string PropertyA { get; } = "override";
    public override string PropertyB { get; protected set; } = "override";
}

How it's done with your current design

Do it like this:

public class Sample : SampleA
{
    public override string PropertyA { get; set; } = "override";
}

or even implement it with more behavior:

public class Sample : SampleA
{
    private string _propertyA = "override";

    public override string PropertyA
    {
        get { return _propertyA; }
        set
        {
            // Maybe do some checks here
            _propertyA = value;
        }
    }
}
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
  • @Nagaraj Hey I improved my answer a little to better match your estimated requirements and issues. Please take some time to read through. :) – LuckyLikey Apr 26 '19 at 07:32
0

When you override your property you should write both get and set. In your case you create a new property with the same name but with another signature.

NoImagination
  • 178
  • 1
  • 8
  • Thanks @Nolmagination that's clear :-) ``` public override string PropertyA { get; set; } = "OK"; ``` – Nagaraj Apr 26 '19 at 06:43
  • 1
    Small nit pick. He has not event created a new Property but a field (because without `get` or `set` it's only a field. – RedX Apr 26 '19 at 06:56