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?
- Security and Integrity – A sealed class cannot be extended, so its behavior remains predictable and cannot be tampered with by subclasses.
- Performance – The runtime can optimize sealed types more aggressively because it knows there are no subclasses.
- Static Utility Types – Many libraries expose static members through sealed classes (e.g.,
Pensin 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
- C# Classes & Objects: Foundations for Robust OOP
- C# Methods Explained: Declaration, Calling, Parameters, Return Types & More
- Mastering C# Abstract Classes & Methods: A Practical Guide
- Understanding C# Partial Classes and Methods
- C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
- C++ Friend Functions and Friend Classes: Mastering Access Control
- Java Classes and Objects: A Practical Guide
- Java Abstract Classes and Methods: A Comprehensive Guide
- Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
- C++ Classes & Objects: A Practical Guide with Code Examples