C# > Object-Oriented Programming (OOP) > Inheritance > Sealed Classes and Methods

Sealed Class Example

This example demonstrates how to define and use sealed classes in C#. Sealed classes prevent inheritance, ensuring a specific implementation cannot be modified or extended by derived classes. This can be useful for security reasons or when you want to strictly control the class hierarchy.

Sealed Class Definition

The sealed keyword before the class keyword prevents any other class from inheriting from FinalClass. Attempting to inherit from a sealed class will result in a compile-time error.

public sealed class FinalClass
{
    public string Message { get; set; }

    public FinalClass(string message)
    {
        Message = message;
    }

    public string GetMessage()
    {
        return Message;
    }
}

Usage Example

This demonstrates how to create an instance of the sealed class and access its members. You can use the sealed class like any other class; the only restriction is that you cannot inherit from it.

public class Example
{
    public static void Main(string[] args)
    {
        FinalClass finalObject = new FinalClass("This is a sealed class.");
        Console.WriteLine(finalObject.GetMessage());
    }
}

Attempting to Inherit (Compile-Time Error)

If you uncomment the code, the compiler will generate an error because you are trying to inherit from a sealed class.

// This code will cause a compile-time error.
// class DerivedClass : FinalClass {}

Concepts behind the snippet

Sealed classes are a fundamental concept in object-oriented programming that provides a way to prevent inheritance. This provides a level of control that is useful to protect critical parts of the application.

Real-Life Use Case

Sealed classes can be used to prevent unauthorized extension of core framework classes or classes containing sensitive data. For example, you might seal a class that handles encryption keys to prevent malicious code from overriding its functionality.

Best Practices

Use sealed classes sparingly. Consider whether future extension might be beneficial before sealing a class. Seal classes only when inheritance is explicitly not desired or for security reasons.

When to use them

  • When you want to prevent inheritance for security reasons.
  • When you have a complete implementation that doesn't need further modification.
  • When you want to improve performance by preventing virtual method calls in derived classes.

pros

  • Security: Prevent malicious code from overriding critical functionality.
  • Performance: Eliminates the overhead of virtual method calls in derived classes (minor improvement).
  • Control: Ensures a specific implementation remains unchanged.

cons

  • Limited extensibility: Prevents future extension of the class.
  • Reduced flexibility: Can make it harder to adapt to changing requirements.

FAQ

  • What happens if I try to inherit from a sealed class?

    You will get a compile-time error. The compiler will prevent you from creating a derived class from a sealed class.
  • Can I use sealed classes with interfaces?

    Yes, you can implement interfaces in sealed classes. The sealed class simply prevents inheritance, but it can still fulfill interface contracts.