C# Sealed Class and Method

In C#, the sealed keyword is used to restrict inheritance from a class or to prevent further modification of a method within a derived class. A sealed class or method cannot be inherited or overridden by any other class or method.

Sealed Class

A sealed class in C# is a class that cannot be inherited by any other class. This means that you cannot create a derived class from a sealed class.

1sealed class Vehicle 2{ 3 // class members 4} 5 6class Car : Vehicle // Error: Cannot derive from sealed type 'Vehicle' 7{ 8 // class members 9}

Sealed Method

A sealed method in C# is a method that cannot be overridden by any derived class. This means that you cannot change the implementation of a sealed method in any derived class.

1class Vehicle 2{ 3 public virtual void Start() 4 { 5 Console.WriteLine("Vehicle started."); 6 } 7} 8 9class Car : Vehicle 10{ 11 public sealed override void Start() 12 { 13 Console.WriteLine("Car started."); 14 } 15} 16 17class SportsCar : Car // Error: Cannot override sealed method 'Car.Start()' 18{ 19 // class members 20}