I learned Java before I learned C#, and I've typically found C# to give me more freedom as a programmer than Java does, and that's why I really like it. I'm a bit baffled though by a problem I'm currently running into concerning access modifiers. Say I have a class.
public class Foo
{
public virtual int SomeProperty { get; protected set; }
}
And another derived class.
public class Bar : Foo
{
public override int SomeProperty { get; set; }
}
The compiler throws an error saying
CS0507 'Bar.SomeProperty.set': cannot change access modifiers when overriding 'protected' inherited member 'Foo.SomeProperty.set'
Why is this a thing? In Java, I am allowed to expand access to base members by overriding them in derived classes, just not the other way around. (Read: You cannot restrict base members further in derived classes) Am I missing something here or doing something stupid I don't know about?
I know I could declare public new SomeProperty { get; set; }
, but that's not what I want. I want polymorphism here.
Please enlighten me. Thank you.