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 is a sealed class in C#?
A sealed class in C# is a class that cannot be inherited by other classes. When you declare a class as sealed, it prevents any other class from deriving from it. The sealed keyword can also be applied to methods to prevent them from being overridden in further derived classes.
Sealed classes are useful when you want to restrict inheritance for security, performance, or design reasons. Common examples include the string class and many value types in .NET Framework.
Syntax
Following is the syntax for declaring a sealed class −
public sealed class ClassName {
// class members
}
Following is the syntax for a sealed method −
public class BaseClass {
public virtual void Method() { }
}
public class DerivedClass : BaseClass {
public sealed override void Method() {
// implementation
}
}
Key Rules of Sealed Classes
-
A sealed class cannot be inherited by any other class.
-
A sealed method must be an override method in a derived class.
-
You can create instances of sealed classes directly.
-
Sealed classes can inherit from other classes but cannot be inherited themselves.
Using Sealed Classes
Example
using System;
public sealed class Result {
public string Display() {
return "Passed";
}
public void ShowStatus(string studentName) {
Console.WriteLine(studentName + " has " + Display());
}
}
class Program {
static void Main(string[] args) {
Result ob = new Result();
string str = ob.Display();
Console.WriteLine(str);
ob.ShowStatus("John");
}
}
The output of the above code is −
Passed John has Passed
Using Sealed Methods
Example
using System;
public class Vehicle {
public virtual void Start() {
Console.WriteLine("Vehicle starting...");
}
}
public class Car : Vehicle {
public sealed override void Start() {
Console.WriteLine("Car engine started");
}
}
public class SportsCar : Car {
// Cannot override Start() method here - it's sealed
public void Accelerate() {
Console.WriteLine("Sports car accelerating fast!");
}
}
class Program {
static void Main(string[] args) {
SportsCar myCar = new SportsCar();
myCar.Start();
myCar.Accelerate();
}
}
The output of the above code is −
Car engine started Sports car accelerating fast!
Sealed vs Abstract Classes
| Sealed Class | Abstract Class |
|---|---|
| Cannot be inherited | Must be inherited to be used |
| Can be instantiated directly | Cannot be instantiated directly |
| Prevents further inheritance | Forces inheritance for implementation |
| All methods have implementations | May contain abstract methods without implementation |
Conclusion
Sealed classes in C# prevent inheritance, making them useful for creating final implementations that cannot be extended. Use sealed classes when you want to restrict further derivation for security, performance, or design integrity reasons.
