Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Master sealed classes and methods in C#—learn when to use them, see clear examples, and boost code safety and performance.

Understanding Sealed Classes and Methods in C#

This guide explains how to use the sealed keyword to restrict inheritance and method overriding in C#, with practical code examples.

Sealed Class

In C#, the sealed keyword prevents a class from being inherited. A sealed class can never have a derived type.

Example:

using System;
namespace SealedClass {
  sealed class Animal {
    
  }

  // Trying to inherit a sealed class results in a compile‑time error
  class Dog : Animal {
    
  }   

  class Program  {
    static void Main (string [] args) {

      // create an object of Dog class
      Dog d1 = new Dog();  
      Console.ReadLine();
    }
  }
}

The compiler emits:

error CS0509: 'Dog': cannot derive from sealed type 'Animal'

Sealed Method

When overriding a virtual method, you may want to prevent further overriding in derived classes. Prefix the override declaration with sealed to achieve this.

Example:

using System;
namespace SealedClass {

  class Animal {
    public virtual void MakeSound() {
      Console.WriteLine(\"Animal Sound\");
    }
  }

  class Dog : Animal {

    // Sealed override
    sealed public override void MakeSound() {
      Console.WriteLine(\"Dog Sound\");
    }
  }

  class Puppy : Dog {

    // Attempting to override a sealed method causes a compile‑time error
    public override void MakeSound() {
      Console.WriteLine(\"Puppy Sound\");
    }
  }   

  class Program  {
    static void Main (string [] args) {
      
      // create an object of Puppy class
      Puppy d1 = new Puppy();  
      Console.ReadLine();
    }
  }
}

Resulting error:

error CS0239: 'Puppy.MakeSound()': cannot override inherited member 'Dog.MakeSound()' because it is sealed

Tip: Sealing an overridden method stops further overriding in a multilevel inheritance chain.


Why Use Sealed Classes?

  1. Security and Integrity – A sealed class cannot be extended, so its behavior remains predictable and cannot be tampered with by subclasses.
  2. Performance – The runtime can optimize sealed types more aggressively because it knows there are no subclasses.
  3. Static Utility Types – Many libraries expose static members through sealed classes (e.g., Pens in System.Drawing).

For example, the Pens class provides pre‑configured Pen instances for standard colors:

Pen bluePen = Pens.Blue; // represents a blue pen

Because Pens is sealed, its static members cannot be overridden or altered by other types.


C Language

  1. C# Classes & Objects: Foundations for Robust OOP
  2. C# Methods Explained: Declaration, Calling, Parameters, Return Types & More
  3. Mastering C# Abstract Classes & Methods: A Practical Guide
  4. Understanding C# Partial Classes and Methods
  5. C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
  6. C++ Friend Functions and Friend Classes: Mastering Access Control
  7. Java Classes and Objects: A Practical Guide
  8. Java Abstract Classes and Methods: A Comprehensive Guide
  9. Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
  10. C++ Classes & Objects: A Practical Guide with Code Examples