Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding setters to properties in overrides

Why is it allowed to change the visibility and existence of getters or setters in a property when implementing an interface?

interface IFoo
{
    string Bar { get; }
}

class RealFoo : IFoo
{
    public RealFoo(string bar)
    {
        this.Bar = bar;
    }

    public string Bar { get; private set; }
}

class StubFoo : IFoo
{
    public string Bar { get; set; }
}

...and not legal to do the same when implementing an abstract class?

abstract class AbstractFoo : IFoo
{
    public abstract string Bar { get; }
}

class RealFoo : AbstractFoo
{
    public RealFoo(string bar)
    {
        this.Bar = bar;
    }

    // Cannot override because 'Bar' does not have an overridable set accessor
    public override string Bar { get; private set; }
}
like image 204
ndeuma Avatar asked May 19 '11 12:05

ndeuma


People also ask

Can we override a getter method?

Overriding a Property Getter MethodIts return type must be the same as the type of the property. It must return the value of the property. To refer to the value of this property, this method must use the variable i%PropertyName.

Can you override a property in C#?

Overriding Properties in C#We can also override the property of a parent class from its child class similar to a method. Like methods, we need to use virtual keyword with the property in the parent class and override keyword with the porperty in the child class.

How to access property in c#?

To the implementer of a class, a property is one or two code blocks, representing a get accessor and/or a set accessor. The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value.

What is get set in c#?

The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. If you don't fully understand it, take a look at the example below.


2 Answers

The interface declares what public properties the class must have (It's just a contract). Which means you need to have those properties, but can add to them.

The abstract class declares the actual structure of those properties. So if you don't have the setter in the abstract base, you can't add to it in the implementation.
When you write the override modifier it looks in the base class for something to override.

like image 191
Yochai Timmer Avatar answered Sep 21 '22 04:09

Yochai Timmer


It perhaps becomes clearer if you think of the getters and setters as the methods that they eventually become.

In the case of the interface you are defining this:

interface IFoo
{
    string GetBar();
}

Which can be read as "all classes that implement this interface must include this method." Both of your classes do:

class RealFoo : IFoo
{
    public string GetBar();
    private void SetBar(string value);
}

they also implement SetBar(), but that is immaterial; they have fulfilled the contract defined by the interface and are valid.

The abstract class, on the other hand is this:

abstract class AbstractFoo : IFoo
{
    public abstract string GetBar();
}

Which means that all child classes must provide a method body for GetBar()

The class you made is this:

class RealFoo : AbstractFoo
{
    public override string GetBar();
    public override void SetBar(string value);
}

By putting the override modifier in front of the SetBar method the compiler is expecting to find an abstract or virtual version in the base class. You don't have that so the compilation fails.

like image 42
Martin Harris Avatar answered Sep 23 '22 04:09

Martin Harris