Sealed Class


In this article we are going to discuss the Sealed class in C#. There are many blogs, articles are available on the internet regarding Sealed class but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Sealed class in C#.

Sealed Class Introduction

  1. In C#, a sealed class is one that cannot be inherited by other classes.
  2. It's the opposite of an abstract class, which is designed to be inherited and extended.
  3. When you mark a class as sealed, you are essentially preventing other classes from deriving from it.
  4. The compiler throws an error if a class is derived from a sealed class.
  5. you have ever noticed, structs are sealed. You cannot derive a class from a struct.

The following class definition defines a sealed class in C#:

//Sealed class
sealed class SealedClass
{
}

Why Sealed Classes?

In C#, when we talk about a sealed class, think of it as a special kind of class that doesn't want to share its stuff. Imagine you have a cool toy, and you don't want anyone else to play with it or change it. That's what a sealed class does - it says, you can't make any new toys based on me.

Now, why would you do that? Well, sometimes you create a class that's already perfect the way it is, and you don't want anyone messing with it. Kind of like when you make a LEGO model exactly how you want it and don't want anyone adding or removing pieces.

One great example of a sealed class in C# is when you have a bunch of tools with different colors, like pens or paintbrushes. You don't want anyone creating new versions of these tools with weird colors or behaviors. So, you seal those classes to keep them exactly as they are.

Real World Example

One of the best uses of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace. They represent pens and brushes with standard colors. When you say Pens.Blue, you're talking about a pen that's always blue, and you can't make it any other way. Same goes for Brushes.Blue - it's always going to be a brush with a blue color. Sealing those classes helps keep things tidy and predictable.

Reason to Use Sealed Class

Typically, you would use a sealed class when you want to prevent further derivation for one of two reasons:

  1. Design Choice - You may design a class to be the final, complete implementation of a concept, and you don't want any further subclasses to modify or extend its behavior.
  2. Security or Performance Concerns - In some cases, allowing subclassing could lead to security vulnerabilities or performance issues. By sealing a class, you ensure that its behavior remains unchanged and predictable.

For Example

Here we create a person class and inherit that class from derived superperson class.

using System;

// Base class representing all humans
sealed class Person
{
    public string Name { get; set; }

    public Person(string name)
    {
        Name = name;
    }

    public void Introduce()
    {
        Console.WriteLine($"Hi, my name is {Name}. I'm just a regular human.");
    }
}

// Sealed class representing superheroes
public class SuperPerson : Person
{
}

class Program
{
    static void Main(string[] args)
    {  
    }
}

Now it is showing, it cannot be inherited by any other class. To see move the mouse over inherrited class person.

Sealed Class

 


Next