What is a Sealed Classes in C#?

Introduction

In C#, the concept of inheritance plays a vital role in object-oriented programming (OOP). Developers often design class hierarchies to create a relationship between different types of objects. However, in some scenarios, you might want to restrict the inheritance of a particular class. This is where sealed classes come into play. In this article, we'll delve into sealed classes in C# and explore their syntax, usage, and benefits, along with practical examples.

What is a Sealed Class?

A sealed class in C# is a class that cannot be inherited. By marking a class as sealed, you prevent other classes from deriving from it. This essentially seals off the inheritance chain, making the class final and unable to be extended further.

Syntax of Sealed Classes

To define a sealed class in C#, you simply use the sealed keyword before the class declaration. Here's the basic syntax:

sealed class SealedClassName
{
    // Class members and methods
}

Example of Sealed Class

Let's consider an example to illustrate the concept of a sealed class.

using System;

// Base class representing a document
class Document
{
    public virtual void Print()
    {
        Console.WriteLine("Printing document.");
    }
}

// Sealed subclass representing a PDF document
sealed class PdfDocument : Document
{
    public override void Print()
    {
        Console.WriteLine("Printing PDF document.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating an instance of the sealed class PdfDocument
        PdfDocument pdf = new PdfDocument();
        pdf.Print(); // Output: Printing PDF document.

        // Attempting to inherit from a sealed class will result in a compilation error
        // class WordDocument : PdfDocument // This line will result in a compilation error
        // {
        //     // Additional members and methods for WordDocument
        // }
    }
}

In this example

  • We have a base class Document representing a generic document with a virtual method Print() that prints "Printing document."
  • We define a sealed subclass PdfDocument that inherits from Document and overrides the Print() method to print "Printing PDF document."
  • In the Main method, we create an instance of the PdfDocument class and invoke its Print() method.
  • Attempting to create a subclass of PdfDocument (like WordDocument in the commented-out code) results in a compilation error because PdfDocument is sealed

Benefits of Sealed Classes

  1. Prevention of Unintended Modifications: Sealed classes provide a clear indication to other developers that the class is not meant to be inherited. This helps prevent unintended modifications to the behavior of the class.
  2. Enhanced Performance: By sealing a class, you allow the compiler and runtime to make certain optimizations, as they know that no further subclassing will occur. This can lead to improved performance in some scenarios.
  3. Security and Stability: Sealed classes contribute to the security and stability of your codebase by limiting the extension points. This reduces the risk of introducing unexpected behaviors or vulnerabilities through subclassing.
  4. Design Intent Clarity: When you seal a class, you explicitly communicate your design intent to other developers. It serves as documentation indicating that the class is complete and not intended for extension.

Conclusion

Sealed classes in C# offer a mechanism to restrict inheritance, providing benefits such as preventing unintended modifications, enhancing performance, ensuring security, and improving design clarity. By sealing a class, you effectively finalize its structure, making it clear that it's not intended to be extended further. While sealed classes are not suitable for every scenario, they play a valuable role in crafting robust and maintainable object-oriented designs. Understanding when and how to use sealed classes can contribute to the overall quality and stability of your C# applications.


Similar Articles