C# sealed Keyword

In C#, the sealed keyword is used to prevent further overriding of a method or class in derived classes. It essentially “locks” the method or class, ensuring that it cannot be overridden or extended any further by subclasses.

sealed keyword use into two ways.

1. Sealing a Method: This prevents further overriding of the method in derived classes.

2. Sealing a Class: This prevents any class from inheriting from the sealed class.

sealed Keyword use in Method

When the sealed keyword is applied to a method, it prevents any class that derives from the class containing the sealed method from overriding that specific method.

Syntax:


public sealed returnType MethodName()
{
    // Implementation
}

Example:


using System;

 class Animal {
    // Virtual method in base class
    public virtual void sound()
    {
        Console.WriteLine("Animal sound.");
    }
}

// Subclass: Dog
class Dog : Animal {
    // Overriding the sound method
    public override void sound() {
        Console.WriteLine("Dog barks");
    }
}

// Subclass: Cat
class Cat : Dog {
    // Overriding the sound method
    public sealed override void sound() {
        Console.WriteLine("Cat meows");
    }
}

// Subclass: Cow
//class Cow : Cat {
    // Overriding the sound method
   // public override void sound() {
   //     Console.WriteLine("Cow Moo");
   // }
//}


public class myProgram {
     static void Main(String[] args) {
        Cat myCat = new Cat();
        myCat.sound();  // Cat meows
    }
}

Note: When uncomment the class Cow then code will show compile time error.

Explanation:

  • Animal Class: The sound() method is defined as virtual, allowing it to be overridden in derived classes.
  • Dog Class: The sound() method is overridden to provide a specific implementation for the dog.
  • Cat Class: The sound() method is sealed, preventing further overriding in any class that derives from Cow.
  • Cow Class: you cannot override the sound() method in any class that inherits from Cat, because it has been sealed.

sealed Keyword use in Class

When you apply the sealed keyword to a class, it prevents any other class from inheriting from it. This is useful when you want to prevent further extension or modification of the class’s behavior.

Syntax:


public sealed class MyClass
{
    // Class implementation
}

Example:


using System;

sealed class LastClass
{
    public void ShowMessage()
    {
        Console.WriteLine("This is a sealed class.");
    }
}

// The following will cause an error because LastClass is sealed
// Error: Cannot derive from sealed class 'LastClass'
// class DerivedClass : LastClass {
   //Console.WriteLine("This is a sealed class.");
 //}  

class MyProgram
{
    static void Main()
    {
        LastClass obj = new LastClass();
        obj.ShowMessage();  // Output: This is a sealed class.
    }
}

Explanation:

  1. LastClass is marked as sealed, so no other class can inherit from it.
  2. If you try to create a derived class from LastClass, you will get a compile-time error.