Sealed Class in C#

A sealed class in C# is a class that cannot be inherited. When a class is marked with the sealed keyword, no other class can derive from it. This is useful when a class is complete, should not be extended, or must protect its behavior from being changed through inheritance.

Sealing a class is a design decision. It communicates that the class is not meant to be used as a base class. This can make code safer because no derived class can override behavior, change assumptions, or misuse internal design.

In C#, sealed classes are commonly used for utility-like final types, immutable value-style objects, security-sensitive classes, performance-sensitive classes, and classes whose inheritance behavior has not been designed or tested.


What Is Sealed Class in C#?

A sealed class is declared using the sealed keyword before the class keyword. It can be instantiated like a normal class, but it cannot be inherited.

sealed class Invoice
{
    public int InvoiceId { get; set; }

    public void Print()
    {
        Console.WriteLine("Printing invoice");
    }
}

You can create an object of Invoice, but you cannot create a class that inherits from it.

Invoice invoice = new Invoice();
invoice.Print();

// Not allowed
// class CustomInvoice : Invoice { }

Syntax of Sealed Class

sealed class ClassName
{
    // members
}

The syntax is simple, but the design meaning is important. The class is saying: this type is final and should not be extended through inheritance.

Why Use Sealed Class in C#?

  • To prevent incorrect inheritance.
  • To protect important behavior from being overridden.
  • To communicate that the class is not designed as a base class.
  • To keep immutable or security-sensitive types stable.
  • To allow some runtime optimizations in certain cases.

Inheritance is powerful, but it also creates responsibility. If a class can be inherited, the base class must be designed to support derived classes safely. If that support is not intended, sealing the class is often clearer.

Sealed Class Example

The following example shows a sealed class used for an immutable user identifier. There is no reason for another class to inherit and change its meaning.

sealed class UserId
{
    public int Value { get; }

    public UserId(int value)
    {
        if (value <= 0)
        {
            throw new ArgumentException("User ID must be positive");
        }

        Value = value;
    }
}

This class has one clear purpose. Sealing it prevents another class from inheriting and changing the meaning of a user ID.

Sealed Method in C#

C# also allows sealing an overridden method. This means a derived class can override a virtual method, and then stop further derived classes from overriding that method again.

class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Animal sound");
    }
}

class Dog : Animal
{
    public sealed override void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

class Puppy : Dog
{
    // Not allowed
    // public override void MakeSound() { }
}

The Dog class overrides MakeSound and seals it. Any class that inherits from Dog cannot override MakeSound again.

Sealed Class vs Static Class

PointSealed ClassStatic Class
Object creationAllowedNot allowed
Instance membersAllowedNot allowed
InheritanceCannot be inheritedCannot be inherited
Use caseFinal object typeUtility or type-level behavior
StateCan have instance stateOnly static state

A sealed class can still represent objects. A static class cannot. This is the key difference. Do not use a static class only because you want to block inheritance.

Sealed Class vs Abstract Class

A sealed class and abstract class are opposite in inheritance design. An abstract class is designed to be inherited. A sealed class is designed not to be inherited.

PointSealed ClassAbstract Class
InstantiationCan be instantiatedCannot be instantiated directly
InheritanceBlocks inheritanceRequires or supports inheritance
PurposeFinal complete typeIncomplete base type
Abstract membersCannot contain abstract membersCan contain abstract members

When to Use Sealed Class

  • Use it when the class is complete and not designed for inheritance.
  • Use it when derived classes could break important behavior.
  • Use it for immutable value-style classes.
  • Use it for security-sensitive types where behavior should not be changed.
  • Use it when you want to communicate final design clearly.

Sealed classes are not only about restriction. They are also about communication. They tell other developers that inheritance is not part of the supported design.

When to Avoid Sealed Class

  • Avoid sealing a class that is intentionally designed as a base class.
  • Avoid sealing too early if extension is a real requirement.
  • Avoid sealing public APIs without considering users who may need inheritance.
  • Avoid using sealed as a replacement for thoughtful class design.

If users of a library reasonably expect to extend a class, sealing it may be too restrictive. The decision depends on whether inheritance is part of the intended contract.

Real World Example of Sealed Class

Imagine a class that represents a validated tax identification number. The rules are strict, and the meaning should not be changed by inheritance. A sealed class can protect the type from unsafe extension.

sealed class TaxId
{
    public string Value { get; }

    public TaxId(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new ArgumentException("Tax ID is required");
        }

        Value = value;
    }

    public override string ToString()
    {
        return Value;
    }
}

This type has a narrow purpose and strict meaning. Sealing it prevents another class from inheriting and changing that meaning unexpectedly.

Sealed Class and Performance

In some cases, sealing a class or method can help the runtime optimize calls because it knows the behavior cannot be overridden further. This can allow devirtualization in certain scenarios. However, performance should not be the main reason beginners use sealed classes.

The main reason to seal a class should be design correctness. If a class is not intended for inheritance, sealing it makes that intent explicit. Any performance benefit is secondary and depends on runtime behavior.

Common Mistakes with Sealed Classes

  • Sealing every class without thinking about extension requirements.
  • Using sealed when an abstract base class is actually needed.
  • Confusing sealed classes with static classes.
  • Sealing public library types without considering API users.
  • Using inheritance prevention to hide poor design decisions.

The keyword should support the design, not replace the design. If a class is sealed, it should be because extension would be unsafe, unnecessary, or unsupported.

Best Practices for Sealed Classes in C#

  • Seal classes that are not designed or tested for inheritance.
  • Use sealed for small final types with strict meaning.
  • Use sealed methods to stop further overriding when needed.
  • Do not seal classes that are meant to be extended by users.
  • Prefer composition if inheritance is not the right relationship.
  • Document the design reason when sealing a public API type.

Sealed Class Design Rule

A good rule is this: leave inheritance open only when the class is intentionally designed for inheritance. Designing for inheritance means thinking about virtual members, protected members, constructor behavior, invariants, and how derived classes may safely extend behavior. If that work has not been done, sealing the class can be the more honest design.

For internal application code, sealing can reduce accidental misuse. For public library code, sealing should be considered more carefully because external users may depend on inheritance for customization.

Sealed Class Interview Points

For interviews, remember that a sealed class cannot be inherited but can be instantiated. A sealed class can contain instance members, constructors, properties, methods, and fields. A sealed class is different from a static class because a static class cannot be instantiated and can contain only static members.

Also remember that sealed can be used with overridden methods. A sealed override method prevents further derived classes from overriding that method again.

Sealed Class and Inheritance Contract

When a class is not sealed, other developers may assume that inheritance is supported. That creates an implicit contract. The base class must then be careful about protected members, virtual methods, constructor order, state validation, and behavior that derived classes might depend on.

If the class author does not want to support that contract, sealed is a clear signal. It avoids accidental inheritance and prevents derived classes from depending on implementation details that may change later.

Sealed Class and Immutable Design

Sealed classes work well with immutable objects. If a class has read-only state and strict validation, inheritance may allow derived classes to weaken those guarantees. Sealing the class helps preserve the meaning of the type.

For example, a validated ID, money value, coordinate, token, or configuration snapshot may be sealed because the type has a precise meaning. The program should be able to trust that meaning wherever the object is used.

Sealed Class and Composition

If a sealed class cannot be inherited, code can still reuse it through composition. Composition means one class contains another class and uses its behavior. This is often safer than inheritance because it avoids tight coupling between base and derived classes.

For example, instead of inheriting from a sealed payment result class, another class can store a payment result object as a property. This keeps the final type protected while still allowing larger objects to use it safely over time as application requirements and development teams grow later.

FAQs on Sealed Class in C#

Can sealed class be inherited in C#?

No. A sealed class cannot be inherited. The compiler gives an error if another class tries to derive from it.

Can we create object of sealed class?

Yes. A sealed class can be instantiated like a normal class unless its constructor is inaccessible.

Is sealed class same as static class?

No. A sealed class can have instance members and objects. A static class cannot be instantiated and can contain only static members.

Can a sealed class have abstract methods?

No. A sealed class cannot contain abstract methods because abstract methods require derived classes, and sealed classes cannot be inherited.