C# Sealed Classes and methods


In C#, sealed classes and sealed methods are used to control inheritance and method overriding, providing a way to limit the extensibility of a class or method. They are essential for ensuring that certain classes or methods maintain their intended behavior without being modified by derived classes.

Sealed Classes

A sealed class is a class that cannot be inherited. Once a class is marked as sealed, no other class can derive from it. This is useful when you want to create a class that should not be extended, often because it represents a complete implementation that should not be modified.

Example of Sealed Class

public sealed class SealedClass { public void DisplayMessage() { Console.WriteLine("This is a sealed class."); } } // Attempting to derive from a sealed class will result in a compilation error // public class DerivedClass : SealedClass // { // } public class Program { public static void Main() { SealedClass obj = new SealedClass(); obj.DisplayMessage(); // Outputs: This is a sealed class. } }

Key Points About Sealed Classes

  • No Inheritance: You cannot derive from a sealed class. This helps in preventing unintended behavior and modifications.
  • Performance: Sealing a class can improve performance in some cases, as the runtime can optimize calls to sealed classes.
  • Use Cases: Sealed classes are often used for utility classes or when a class represents a complete concept that should not be extended.

Sealed Methods

A sealed method is a method that cannot be overridden in any derived class. This is useful when you want to ensure that the method's implementation remains unchanged in any subclasses.

Example of Sealed Method

To define a sealed method, you must first have a method in a base class marked as virtual, and then you can override it in a derived class and seal it.

public class BaseClass { public virtual void Display() { Console.WriteLine("Display from BaseClass."); } } public class DerivedClass : BaseClass { public sealed override void Display() { Console.WriteLine("Display from DerivedClass."); } } // Attempting to derive from DerivedClass and override the Display method will result in a compilation error // public class FurtherDerivedClass : DerivedClass // { // public override void Display() // Error: cannot override inherited member // { // } // } public class Program { public static void Main() { BaseClass obj = new DerivedClass(); obj.Display(); // Outputs: Display from DerivedClass. } }

Key Points About Sealed Methods

  • Prevents Further Overriding: Sealed methods cannot be overridden in any derived classes, maintaining the behavior defined in the class where it was sealed.
  • Inherits Base Behavior: Derived classes can still inherit and use the sealed method, but they cannot change its implementation.
  • Use Cases: Sealing methods is useful in scenarios where maintaining specific functionality is critical, such as in libraries or APIs.

Summary

Sealed classes and methods in C# provide mechanisms to enforce control over inheritance and method overriding.

  • Sealed Classes prevent further inheritance, ensuring that the class's functionality remains intact and optimized.
  • Sealed Methods prevent derived classes from changing the behavior of specific methods, allowing for predictable and consistent behavior.

Both features are useful for maintaining code integrity and ensuring that certain parts of the codebase behave as intended without unintended modifications.