Understanding Sealed Classes in C# Inheritance
Quick Answer
In C#, a sealed class is a class that cannot be inherited. Marking a class as sealed prevents other classes from deriving from it, which can improve security and performance by restricting extensibility.
Learning Objectives
- Explain the purpose of Sealed Classes in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Sealed Classes.
- Apply Sealed Classes in a simple real-world scenario or practice task.
Introduction
Inheritance is a core concept in object-oriented programming that allows a class to inherit members from another class.
In C#, the sealed keyword is used to restrict inheritance by preventing other classes from deriving from a sealed class.
Understanding sealed classes helps you control class hierarchies and improve code safety.
Sealing a class is like closing the door to further inheritance.
What Are Sealed Classes?
A sealed class in C# is a class that cannot be used as a base class. This means no other class can inherit from it.
Sealing a class is useful when you want to prevent modification of behavior through inheritance.
- Declared using the sealed keyword before the class name.
- Prevents further derivation.
- Can improve performance by enabling certain compiler optimizations.
How to Declare a Sealed Class
To declare a sealed class, use the sealed keyword in the class declaration.
Attempting to inherit from a sealed class results in a compile-time error.
Example of a Sealed Class
Here is a simple example demonstrating a sealed class and an attempt to inherit from it.
When to Use Sealed Classes
Sealed classes are appropriate when you want to:
Limit the inheritance hierarchy to prevent unintended behavior changes.
Enhance security by restricting extensibility.
Improve performance by allowing the runtime to optimize calls.
- Finalizing a class design to avoid misuse.
- Preventing inheritance from classes that are not designed for extension.
- Avoiding complexity in large class hierarchies.
Sealed Classes and Methods
In addition to sealing classes, C# allows sealing of methods to prevent overriding in derived classes.
A sealed method can only be declared in a class that overrides a virtual method.
- Use sealed keyword with override to prevent further overriding.
- Sealing methods controls polymorphic behavior.
Practical Example
The Vehicle class is sealed, so no class can inherit from it. Attempting to derive Car from Vehicle will cause a compile-time error.
The Display method in Derived is sealed, preventing any further overriding in subclasses.
Examples
sealed class Vehicle
{
public void Start() {
Console.WriteLine("Vehicle started.");
}
}
// This will cause a compile-time error:
// class Car : Vehicle {}
The Vehicle class is sealed, so no class can inherit from it. Attempting to derive Car from Vehicle will cause a compile-time error.
class Base
{
public virtual void Display() {
Console.WriteLine("Base Display");
}
}
class Derived : Base
{
public sealed override void Display() {
Console.WriteLine("Derived Display");
}
}
// Further inheritance cannot override Display method
class MoreDerived : Derived
{
// public override void Display() {} // Compile-time error
}
The Display method in Derived is sealed, preventing any further overriding in subclasses.
Best Practices
- Seal classes when you want to prevent inheritance to maintain control over behavior.
- Use sealed methods to stop further overriding of critical methods.
- Avoid sealing classes prematurely if future extensibility is expected.
- Document why a class or method is sealed to inform other developers.
Common Mistakes
- Trying to inherit from a sealed class, which causes compile-time errors.
- Sealing classes or methods without a clear reason, limiting flexibility unnecessarily.
- Confusing sealed classes with abstract classes; sealed classes cannot be inherited, abstract classes must be inherited.
- Forgetting that sealed methods can only be declared in overriding methods.
Hands-on Exercise
Create and Use a Sealed Class
Write a sealed class named 'Logger' with a method 'Log' that prints a message. Attempt to inherit from 'Logger' and observe the compiler error.
Expected output: A compile-time error when trying to inherit from the sealed Logger class.
Hint: Use the sealed keyword before the class name.
Seal an Overridden Method
Create a base class with a virtual method. Override it in a derived class and seal the override. Try to override it again in another subclass and note the error.
Expected output: A compile-time error when attempting to override the sealed method.
Hint: Use sealed override keywords on the method in the derived class.
Interview Questions
What is a sealed class in C# and why would you use it?
InterviewA sealed class in C# is a class that cannot be inherited. It is used to prevent further derivation, which can help maintain control over class behavior, improve security, and enable performance optimizations.
Can you inherit from a sealed class in C#?
InterviewNo, attempting to inherit from a sealed class results in a compile-time error.
How do sealed methods work in C#?
InterviewSealed methods prevent further overriding in derived classes. They are declared by combining the sealed and override keywords in a method that overrides a virtual method.
MCQ Quiz
1. What is the best first step when learning Sealed Classes?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Sealed Classes?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. In C#, a sealed class is a class that cannot be inherited.
B. Sealed Classes never needs examples
C. Sealed Classes is unrelated to practical work
D. Sealed Classes should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- In C#, a sealed class is a class that cannot be inherited.
- Marking a class as sealed prevents other classes from deriving from it, which can improve security and performance by restricting extensibility.
- Inheritance is a core concept in object-oriented programming that allows a class to inherit members from another class.
- In C#, the sealed keyword is used to restrict inheritance by preventing other classes from deriving from a sealed class.
- Understanding sealed classes helps you control class hierarchies and improve code safety.
Summary
Sealed classes in C# prevent other classes from inheriting from them, providing control over class hierarchies.
They are declared using the sealed keyword and help improve security and performance.
Sealed methods prevent further overriding and help maintain consistent behavior in inheritance chains.
Understanding when and how to use sealed classes and methods is essential for robust C# programming.
Frequently Asked Questions
Can a sealed class be instantiated?
Yes, a sealed class can be instantiated like any other class; sealing only prevents inheritance.
Is it possible to seal an abstract class?
No, sealing an abstract class is not allowed because abstract classes are meant to be inherited.
Does sealing a class improve performance?
Sealing a class can improve performance by allowing certain runtime optimizations, such as devirtualization of method calls.
Can interfaces be sealed in C#?
No, interfaces cannot be sealed because they are contracts meant to be implemented by multiple classes.
What is Sealed Classes?
In C#, a sealed class is a class that cannot be inherited.
Why is Sealed Classes important?
Marking a class as sealed prevents other classes from deriving from it, which can improve security and performance by restricting extensibility.

