Sealed Class in C# with examples

csharp-sealed-class
sealed class in C#

A sealed class in C# is a class that cannot be inherited or modified by any other class but can still be instantiated. It is not allowed to have any derived classes. Sealed classes are commonly used to prevent accidental inheritance, ensuring that the class remains as it is without any modifications.

The main purpose of a sealed class is to indicate that it is highly specialized and does not require any additional functionality through inheritance. It means that it cannot be extended to add new behaviors or modify its existing behavior.

Points to remember about sealed class in C#

  1. Cannot be inherited: A sealed class is declared using the sealed keyword, which prevents other classes from inheriting it. It ensures that the sealed class remains as it is and cannot be extended or modified through inheritance.
  2. Allows instantiation: Despite not being inheritable, a sealed class can still be instantiated, meaning objects of the sealed class can be created and used in the program.
  3. Specialized and self-contained: The design intent of a sealed class is to indicate that it serves a specific purpose or encapsulates a certain logic. It is intended to be self-contained and doesn’t require additional functionality through inheritance.
  4. Prevents overriding: By sealing a class, you prevent derived classes from overriding its behavior by overriding its methods or properties. It ensures that the sealed class’s implementation remains unchanged.
  5. Used for encapsulation: Sealed classes are commonly used to encapsulate a specific set of functionality or logic that needs to be used across the program. They provide a way to package and reuse code without allowing modifications or extensions.
  6. Performance benefits: Sealed classes can provide performance benefits since the compiler can optimize calls to sealed methods more efficiently than virtual or overridden methods.
  7. Design choice: Choosing to make a class sealed should be based on carefully considering the class’s purpose and the need for inheritance. If a class is not intended to be extended or modified, making it sealed can help improve code maintainability and prevent unintended changes.

Sealed Class Example:

The following is an example of Sealed class in C#.

using System;
sealed class MySealedClass
{
    public int num1;
    public int num2;
}

class Program
{
    // Main Method
    static void Main()
    {
        // Creating an object of Sealed Class
        MySealedClass sc = new()
        {
            num1 = 100,
            num2 = 200

        };
        // Printing value of num1 and num2
        Console.WriteLine($"num1 = {sc.num1}, num2 = {sc.num2}");
    }
}
// Output: num1 = 100, num2 = 200

Now, if we try to inherit a child class from a sealed class, an error will be produced stating that ‘ChildClass’: cannot derive from the sealed type ‘MySealedClass’.

CSharp sealed class example
Example: sealed class

C# Sealed method

In C#, a sealed method is a method that is declared within a class and cannot be overridden by derived classes. It must be declared using sealed and override keywords. When a method is marked as sealed using the sealed keyword, it prevents any further modification or extension of that method in subclasses.

It is useful when you want to restrict the ability to override specific methods in a class hierarchy to maintain consistency or security.

Sealed method example:

In the code below, if you want to restrict the Child2 class from overriding the Display method, you must use the sealed modifier with the override modifier in the Child1 class. This will prevent the method from being overridden in derived classes.

class Parent
{
    public virtual void Display() { }
}
class Child1 : Parent
{
    public sealed override void Display() { }
}
class Child2 : Child1
{
    // 'Child2.Display()': cannot override inherited member 'Child1.Display()' because it is sealed
    public override void Display() { }
}

FAQs:

Q: What is a sealed class in C#? 

A sealed class in C# is a special type of class that cannot be inherited by other classes. It is marked with the sealed keyword and is designed to serve a specific purpose without allowing modifications or extensions through inheritance.

Q: Why would I use a sealed class?

Sealed classes are used when you want to create a class that should not be inherited or modified. They are typically employed to encapsulate a specific logic or functionality that can be used throughout a program without alteration.

Q: Can we create an object of a sealed class in C#?

Yes, even though a sealed class cannot be inherited, you can still create instances (objects) of the sealed class and use them in your program.

Q: Can you override a sealed class in C#?

You cannot inherit from a sealed class, so no inheritance, no override.

Q: What is the advantage of the sealed keyword in C#?

The sealed keyword in C# allows you to prevent the inheritance of a class or certain class members that were previously marked as virtual.

Q: Are sealed classes beneficial for performance?

Sealed classes can provide performance benefits. Since sealed methods cannot be overridden, the compiler can optimize calls to these methods more efficiently than virtual or overridden methods.

Q: Can a sealed class inherit from another class?

Yes, a sealed class can inherit from another class. However, other classes cannot further inherit the sealed class itself.

Q: How do I create a sealed class in C#?

Use the sealed keyword before the class declaration to create a sealed class. It marks the class as sealed and prevents inheritance by other classes.

References: MSDN- sealed (C# Reference), stackoverflow-Sealed method in C#

Articles to Check Out:

I hope you liked reading this article “sealed class in C#”. Please leave a comment below if you discover an error or would like to provide more details about the subject covered here.

Shekh Ali
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments