Table of Contents
What exactly is a sealed class?
A sealed class is a class that cannot be inherited by any class but can be instantiated. A sealed class can’t have any derived classes. A common use for a sealed class is to prevent a class from being accidentally inherited.
The purpose of a sealed class is to show that it is highly specialized and does not need to extend to add new functionality through inheritance. This is because it modifies or overrides its behavior.
Sealed classes are used to group related classes together and to limit the extensibility of these classes. For example, the System.IO namespace contains a number of sealed classes( Example: FileInfo, DirectoryInfo, BufferedStream, etc. ) that are used for input/output operations.
- Sealed classes can be declared with the “sealed” keyword.
- A sealed class cannot be used as a base class for any other classes.
When a class is marked as sealed, the compiler is informed that it cannot be extended. So, the compiler throws an error if a class derives from a sealed class.
Example:
The class definition for a sealed class in C# is as follows:
Create a sealed class called “MySealedClass” in the code below, then utilise it in Program. It will work properly if you run this code.
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’.
C# Sealed method
In C#, the sealed method cannot be overridden further. It must be used with the override keyword in the method.
A sealed method is used to ensure that no further changes will occur in the behavior of any derived class with respect to this method.
- A sealed method in C# is used to define the overriding level of a virtual method.
- A sealed keyword is always used with the override keyword.
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() { }
}
Sr.No. | Sealed Class and Sealed Method |
---|---|
1. | A sealed class does not necessarily need to have sealed methods as it cannot be derived or extended by any classes. |
2. | Declaring a method as sealed in an unsealed class prevents it from being further overridden by derived classes. |
3. | In C# programming, sealed classes and sealed methods are used to prevent developers from mistakenly extending classes or overriding methods. |
FAQ:
Q: Can a sealed class be instantiated in C#?
Ans: A sealed class in C# can be instantiated however it cannot be inherited by any class.
Q: Can a sealed class be extended?
Ans: A sealed class cannot be extended as no class can be derived from a sealed class.
Q: Can we create an object of a sealed class in C#?
Ans: Yes, we can create an instance of the sealed class.
Q: Can you override a sealed class in C#?
Ans: You canot inherit from a sealed class, so no inheritance, no override.
Q: What is the advantage of the sealed keyword in C#?
Ans: The sealed keyword in C# allows you to prevent the inheritance of a class or certain class members that were previously marked as virtual.
Summary:
In summary, we can conclude that sealed
classes or sealed methods are extremely helpful when we don`t want to expose the functionality to the derived class. In addition, they are useful when you want to restrict your code so developers can’t extend it. A sealed class cannot be derived by any other class and a sealed method cannot be overridden.
References: MSDN- sealed (C# Reference), stackoverflow-Sealed method in C#
Articles to Check Out:
- C# Abstract class Vs Interface
- Generic Delegates in C# With Examples
- Serialization and Deserialization in C# with Example
- C# Array vs List: When should you use an array or a List?
- C# Monitor class in multithreading
- C# Struct vs Class
- C# Dictionary with Examples
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.
- SQL Server Indexing: Clustered vs Non Clustered Index Explained - March 26, 2023
- Mastering Database Normalization: Best Practices and Techniques - March 25, 2023
- CRM Databases: The Key to Enhanced Customer Engagement and Sales - March 23, 2023