Consider Code Sample Below of a simple console application.
Here I tried to restrict access modifiers in derived class for method GetClassName that one is also not allowed.
Moral of the story is in csharp keep access modifier of derived class member same as base class if member is virtual method/function.
Code:
namespace AccessiblityAndMemberClass { class Program { static void Main(string[] args) { } } class MyBaseClass { protected virtual string GetClassName() { return "MyBaseClass"; } } class MyDerivedClass : MyBaseClass { public override string GetClassName() { return "MyDerivedClass"; } } } Here MyBaseClass is Base class having protected virtual method "GetClassName",now when "MyDerivedClass" class which inherit from "MyBaseClass" try to override this method by widening access modifier code doesn't compile This Scenario explains in derived class can't widen restrictive access modifier if specified in Base class. Now consider one more code sample class MyBaseClass1 { public virtual string GetClassName() { return "MyBaseClass"; } } class MyDerivedClass1 : MyBaseClass1 { override string GetClassName() { return "MyDerivedClass"; } }
Moral of the story is in csharp keep access modifier of derived class member same as base class if member is virtual method/function.