C# Sealed Class

When designing object-oriented systems, there are times when you need to restrict a class from being inherited to safeguard its core functionality or logic. This is where a Sealed Class in C# comes into play.
In this lesson, we’ll dive deep into what a sealed class is, understand when and why it’s used, explore its key advantages, and look at practical real-world examples — ending with the most commonly asked C# interview questions on sealed classes.

What is a Sealed Class in C#?

A Sealed Class in C# is a special type of class declared using the sealed keyword. Once a class is marked as sealed, it cannot be inherited by any other class — meaning its implementation and behavior remain protected and unaltered.

When to use sealed classes:

  • Restricting inheritance to maintain security, design integrity, and predictable behavior within your application.
  • Preventing accidental overrides of essential methods that could compromise or alter the intended functionality.
  • Defining a final, complete class where no further extension or customization is needed.

Key Points About Sealed Classes

  • A sealed class cannot act as a base class for other classes.
  • It can inherit from another class, but no other class can inherit from it.
  • You can seal individual methods in a derived class to prevent any further overriding(overriden methods).

Example:


public class PaymentGateway
{
    public virtual void ValidateTransaction()
    {
        Console.WriteLine("Generic validation...");
    }
}

public sealed class SecureGateway : PaymentGateway
{
    public override void ValidateTransaction()
    {
        Console.WriteLine("Secure validation with encryption...");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        PaymentGateway normalGateway = new PaymentGateway();
        normalGateway.ValidateTransaction();

        SecureGateway secureGateway = new SecureGateway();
        secureGateway.ValidateTransaction();
    }
}

Output:

Generic validation...
Secure validation with encryption...

If you try to inherit SecureGateway, the compiler throws an error:

class TestGateway : SecureGateway { } // ❌ Error: cannot derive from sealed class


Output:

❌ Error: cannot derive from sealed class


C# Sealed Class Interview Questions and Answers

If you're preparing for a .NET or C# developer interview, understanding sealed classes is crucial. Sealed classes often come up when discussing inheritance, performance, and runtime optimizations in object-oriented programming.

Below are the top 10 C# Sealed Class Interview Questions with clear and concise answers.


1. What is a sealed class in C#?

A sealed class is a class declared using the sealed keyword that cannot be inherited by any other class. It helps restrict inheritance and lock down the implementation of a class.


2. Why do we use sealed classes?

We use sealed classes to:

  • Prevent other classes from modifying behavior through inheritance.
  • Protect sensitive or core functionality.
  • Improve runtime performance via compiler optimizations.

3. Can a sealed class be abstract?

No. An abstract class is designed to be inherited, while a sealed class prevents inheritance. Both serve opposite purposes and cannot be combined.


4. Can a sealed class implement an interface?

Yes. Even though a sealed class cannot be inherited, it can implement one or more interfaces to define contracts it must fulfill.


5. Can a sealed class contain virtual methods?

No. Virtual methods are intended to be overridden in derived classes. Since sealed classes cannot have derived classes, they cannot contain virtual methods.


6. Can we create an object of a sealed class?

Yes. A sealed class can be instantiated like any normal class. Only inheritance is restricted, not object creation.


7. How do sealed methods differ from sealed classes?

Aspect Description
Sealed Method Prevents further overriding in a derived class.
Sealed Class Prevents any class from inheriting it altogether.

8. What happens if you try to inherit a sealed class?

You’ll encounter a compile-time error:

Cannot derive from sealed type 'ClassName'

This ensures the sealed class remains final and cannot be extended.


9. What are some examples of sealed classes in the .NET Framework?

Several built-in .NET classes are sealed, including:

  • System.String
  • System.Math
  • System.DateTime

These are sealed to prevent modification of core framework behaviors.


10. How does sealing a class improve performance?

When a class is sealed, the JIT compiler knows it cannot be inherited. This allows it to perform de-virtualization — resolving method calls faster at runtime — which improves performance slightly.


Conclusion

Sealed classes are powerful tools in C# for protecting code integrity and ensuring stable, optimized runtime behavior. They’re often used when you want to prevent class extension or lock down specific functionality. It’s ideal for securing APIs, optimizing performance, and ensuring that business rules remain consistent. Use it thoughtfully to balance flexibility with stability.