Sealed

The sealed keyword in C# is used to prevent a class from being inherited. When a class is declared as sealed, it cannot serve as a base class for other classes. This is beneficial when you want to protect your class implementation from being extended or modified through inheritance. Additionally, you can apply the sealed keyword to methods to prevent them from being overridden in derived classes.

Basics of Sealed Classes

  • Definition: A sealed class is declared by placing the sealed keyword before the class keyword.
  • Inheritance: Sealed classes cannot be inherited, ensuring that the class’s behavior remains consistent and unchanged.
  • Sealed Methods: You can also seal methods in a derived class to prevent further overriding. This is done by using the sealed keyword in conjunction with the override keyword.

Example

using System;

// Base class
class Vehicle
{
  public virtual void DisplayInfo()
  {
      Console.WriteLine("This is a vehicle.");
  }
}

// Derived class inheriting from Vehicle

class Car : Vehicle
{
  public sealed override void DisplayInfo()
  {
      Console.WriteLine("This is a car.");
  }
}

// Attempt to further derive from Car

// This will result in a compilation error because Car is sealed
// class SportsCar : Car
// {
//     public override void DisplayInfo()
//     {
//         Console.WriteLine("This is a sports car.");
//     }
// }

class Program
{
  static void Main()
  {
      Car myCar = new Car();
      myCar.DisplayInfo(); // Outputs: This is a car.

        // The following line would cause a compilation error if uncommented
      // because Car is sealed and cannot be inherited from.
      // SportsCar mySportsCar = new SportsCar();
      // mySportsCar.DisplayInfo();
  }
}

Explanation of the Example

  1. Base Class (Vehicle):
    • The Vehicle class has a virtual method DisplayInfo which can be overridden by derived classes.
  2. Derived Class (Car):
    • The Car class inherits from Vehicle and overrides the DisplayInfo method.
    • The DisplayInfo method in the Car class is marked as sealed, preventing further overriding by any classes that might attempt to inherit from Car.
  3. Attempt to Inherit from Sealed Class (Car):
    • The commented SportsCar class attempts to inherit from Car and override the DisplayInfo method. This is not allowed because Car is a sealed class.
    • Attempting to uncomment the SportsCar class definition and its usage would result in a compilation error.

Key Benefits of Using Sealed Classes

  • Security: Sealed classes enhance security by preventing the modification of class behavior. This ensures that the original implementation of the class remains intact and unaltered.
  • Performance: In some cases, using sealed classes can improve performance. This is because the runtime can optimize code execution, knowing that certain methods and classes will not have additional derived types.
  • Design Intention: Employing sealed classes clearly communicates your design intentions, indicating that the class is complete and should not be extended.
Post a comment

Leave a Comment

Scroll to Top