Skip to main content

How to Resolve Error "CS0507: cannot change access modifiers when overriding inherited member" in C#

The Compiler Error CS0507 is an inheritance consistency error. The message reads: " 'Child.Method' : cannot change access modifiers when overriding 'public/protected' inherited member 'Parent.Method'".

In C#, when you override a virtual or abstract member, the access modifier (visibility) of the overriding member must match the base member exactly. You cannot make the member more private (hiding it) or more public (exposing it) than defined in the base contract. This ensures that the derived class can be used interchangeably with the base class without violating security or accessibility expectations.

This guide explains the rules of access modifiers in inheritance and how to align your class definitions.

Understanding Access Consistency

If a base class promises that a method DoWork() is protected, any code consuming that base class expects it to be accessible only to children. If a child overrides it and makes it public, it changes the contract. Conversely, if a base class says DoWork() is public, a child cannot make it private, because that would break the code relying on the public interface of the parent.

note

C# vs. Java: Unlike Java, which allows you to "widen" access (e.g., changing protected to public), C# strictly requires an exact match.

Scenario 1: Trying to Expose a Protected Member (Widening)

This is a common request: "The parent has a protected method, but I want to let everyone call it on my new object." You cannot do this via override.

Example of error:

public class BaseClass
{
// Defined as protected
protected virtual void Initialize()
{
Console.WriteLine("Base Init");
}
}

public class DerivedClass : BaseClass
{
// ⛔️ Error CS0507: cannot change access modifiers when overriding
// 'protected' inherited member 'BaseClass.Initialize()'.
// You cannot change 'protected' to 'public'.
public override void Initialize()
{
Console.WriteLine("Derived Init");
}
}

Scenario 2: Trying to Hide a Public Member (Narrowing)

Similarly, you cannot take a public method from the parent and hide it inside the child by making it protected or private. If the parent says it is public, it must remain public.

Example of error:

public class BaseClass
{
public virtual void Render() { }
}

public class DerivedClass : BaseClass
{
// ⛔️ Error CS0507: cannot change access modifiers.
// 'Render' is public in the base, so it must be public here.
protected override void Render() { }
}

Solution: Match the Modifier Exactly

To fix the error, simply copy the access modifier from the base class to the derived class.

Solution:

public class BaseClass
{
protected virtual void Initialize() { }
}

public class DerivedClass : BaseClass
{
// ✅ Correct: Both are 'protected'.
protected override void Initialize()
{
base.Initialize();
}

// IF you really need a public method, create a new one that wraps the protected call:
public void PublicInitialize()
{
this.Initialize();
}
}
tip

Shadowing Exception: If you strictly intend to change the visibility and you do not care about polymorphism (meaning you don't need baseObject.Method() to call your child code), you can use the new keyword instead of override. This breaks the inheritance chain and allows you to define any access level you want.

Conclusion

CS0507 enforces a strict contract between Parent and Child.

  1. Check the Parent: Look at the definition of the method in the base class.
  2. Match the Child: If the parent is protected, the override must be protected. If the parent is public, the override must be public.
  3. Alternative: If you need to expose a protected method, create a new public "wrapper" method (public void DoInit() => this.Initialize();) instead of trying to change the visibility of the override itself.