Skip to main content

How to Resolve Error "CS0239: 'member' : cannot override inherited member 'inherited member' because it is sealed" in C#

The Compiler Error CS0239 is an inheritance restriction error. The message reads: "'ChildClass.Method' : cannot override inherited member 'ParentClass.Method' because it is sealed".

In C#, the sealed keyword is used to stop the chain of inheritance. When applied to a method, property, or event, it signifies that the member is final. The developer of the parent class explicitly decided that no further modifications to this logic should be allowed in subclasses.

This guide explains how sealed works in inheritance chains and how to handle situations where you need to extend a sealed member.

Understanding the Inheritance Chain

To reach this error, you usually have a three-level inheritance hierarchy:

  1. Level 1 (Base): Defines a virtual member.
  2. Level 2 (Parent): Overrides that member and marks it sealed override. This locks the implementation.
  3. Level 3 (Child): Tries to override it again. CS0239 occurs here.

The sealed modifier effectively says: "The polymorphism stops here."

Scenario: Attempting to Override a Sealed Member

In this example, the Manager class has decided that CalculatePay is final. The Director class tries to change it but is blocked by the compiler.

Example of error:

public class Employee
{
public virtual void CalculatePay() => System.Console.WriteLine("Standard Pay");
}

public class Manager : Employee
{
// The 'sealed' keyword stops future classes from overriding this.
public sealed override void CalculatePay() => System.Console.WriteLine("Manager Pay");
}

public class Director : Manager
{
// ⛔️ Error CS0239: 'Director.CalculatePay()': cannot override inherited member
// 'Manager.CalculatePay()' because it is sealed.
public override void CalculatePay()
{
System.Console.WriteLine("Director Pay");
}
}

Solution 1: Remove the 'override' Keyword (Respect the Design)

Often, this error indicates a design misunderstanding. If the parent class marked the method as sealed, it usually means the logic is critical and shouldn't be changed.

If you don't actually need to change the logic, simply remove your method from the child class and let it inherit the parent's behavior.

public class Director : Manager
{
// ✅ Correct: Removed the override.
// 'Director' instances will use the 'Manager' implementation of CalculatePay.
}

Solution 2: Use 'new' (Shadowing)

If you absolutely need a method with the same name in your child class, but you accept that it will not be polymorphic (meaning it won't run if the object is cast to the base class), you can use Method Shadowing.

Use the new keyword instead of override.

public class Director : Manager
{
// ✅ Correct: 'new' breaks the inheritance chain.
// This creates a brand new method that just happens to be named 'CalculatePay'.
public new void CalculatePay()
{
System.Console.WriteLine("Director Pay");
}
}

// Usage Warning:
// Director d = new Director();
// d.CalculatePay(); // Prints "Director Pay"
//
// Manager m = new Director();
// m.CalculatePay(); // Prints "Manager Pay" (The sealed version)

Solution 3: Unseal the Base Member (If You Own the Code)

If you have access to the source code of the parent class (Manager in the example above) and you believe the sealed restriction is unnecessary, you can simply remove the keyword.

Solution:

public class Manager : Employee
{
// ✅ Correct: Removed 'sealed'. Now derived classes can override it.
public override void CalculatePay() => System.Console.WriteLine("Manager Pay");
}

public class Director : Manager
{
// Now valid
public override void CalculatePay() => System.Console.WriteLine("Director Pay");
}

Conclusion

CS0239 ensures that the "final" decision made by a parent class is respected.

  1. Check the Parent: Verify if the method you are trying to override is marked sealed override.
  2. Decide Intent:
    • Modify Logic Globally: You cannot do this unless you can edit the parent class to remove sealed.
    • Add New Logic Locally: Use the new keyword to shadow the method (breaking polymorphism).
    • Accept Logic: Remove your override and use the parent's implementation.