Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are sealed modifiers in C#?
The sealed modifier in C# prevents method overriding in derived classes. When applied to an overridden method, it stops further inheritance of that method. The sealed method must be part of a derived class and must override a virtual or abstract method from its base class.
Syntax
Following is the syntax for declaring a sealed method −
public sealed override ReturnType MethodName() {
// method implementation
}
Key Rules for Sealed Methods
-
A sealed method must be an override of a virtual or abstract method.
-
Once sealed, the method cannot be overridden in any further derived class.
-
The
sealedmodifier is used together with theoverridemodifier. -
You cannot seal a method that is not already overriding another method.
Example with Compilation Error
The following example demonstrates how a sealed method prevents further overriding −
using System;
class ClassOne {
public virtual void display() {
Console.WriteLine("BaseClass - ClassOne");
}
}
class ClassTwo : ClassOne {
public sealed override void display() {
Console.WriteLine("ClassTwo - Derived Class with sealed method");
}
}
// This will cause a compilation error
/*
class ClassThree : ClassTwo {
public override void display() { // ERROR: Cannot override sealed method
Console.WriteLine("ClassThree - Another Derived Class");
}
}
*/
class Program {
public static void Main() {
ClassOne obj1 = new ClassOne();
ClassTwo obj2 = new ClassTwo();
obj1.display();
obj2.display();
}
}
The output of the above code is −
BaseClass - ClassOne ClassTwo - Derived Class with sealed method
Working Example with Sealed Method
Here's a practical example showing how sealed methods work in an inheritance hierarchy −
using System;
class Shape {
public virtual void Draw() {
Console.WriteLine("Drawing a generic shape");
}
public virtual void CalculateArea() {
Console.WriteLine("Calculating area of generic shape");
}
}
class Rectangle : Shape {
public sealed override void Draw() {
Console.WriteLine("Drawing a rectangle with four sides");
}
public override void CalculateArea() {
Console.WriteLine("Area = length * width");
}
}
class Square : Rectangle {
// Cannot override Draw() because it's sealed in Rectangle
// public override void Draw() { } // This would cause error
public override void CalculateArea() {
Console.WriteLine("Area = side * side");
}
}
class Program {
public static void Main() {
Shape shape = new Square();
shape.Draw(); // Uses Rectangle's sealed implementation
shape.CalculateArea(); // Uses Square's override
}
}
The output of the above code is −
Drawing a rectangle with four sides Area = side * side
Sealed Classes vs Sealed Methods
| Sealed Class | Sealed Method |
|---|---|
| Prevents the entire class from being inherited | Prevents only specific methods from being overridden |
| Applied at class level | Applied at method level |
Example: sealed class MyClass
|
Example: public sealed override void Method()
|
Conclusion
The sealed modifier in C# provides control over method inheritance by preventing further overriding of virtual methods. It must be used with the override keyword and helps maintain the integrity of specific method implementations in inheritance hierarchies.
