I learned how to inherit methods by adding virtual
to the method in the base class and override
in the new class. But what do I do to inherit properties?
class bird
{
private virtual string fly = "Yes, I can!";
public string CanI() { return fly ; }
}
class penguin : bird
{
private override string fly = "No, I can't!";
}
This pops an error, saying that modifiers virtual
/override
should not be used here.
This code:
is creating a field, not a property. Also, in order to be virtual, your property must have access higher than 'private'. You probably want something like this:
That is not a property in your example, it is a field. Try using a property, or simply marking fly as protected so it can be accessed in your subclass.
fly
is not a property, it is a field. Fields are not overrideable. You can do this:Note that I had to mark
fly
asprotected
.But even better, I would do something like this: