Sealed classes and methods stop other classes from changing or extending them. This helps keep your code safe and clear.
Sealed classes and methods in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
sealed class ClassName { // class members } // class DerivedClass : ClassName // This will cause an error // { // } class BaseClass { public virtual void MethodName() { } } class DerivedClass : BaseClass { public sealed override void MethodName() { } } class MoreDerivedClass : DerivedClass { // Cannot override MethodName here }
A sealed class cannot be inherited by any other class.
A sealed method cannot be overridden by any further derived classes.
Examples
Animal. No class can inherit from it.C Sharp (C#)
sealed class Animal { public void Speak() { Console.WriteLine("Animal speaks"); } } // Trying to inherit Animal will cause an error // class Dog : Animal { }
Start in class Car. It cannot be changed in SportsCar.C Sharp (C#)
class Vehicle { public virtual void Start() { Console.WriteLine("Vehicle starts"); } } class Car : Vehicle { public sealed override void Start() { Console.WriteLine("Car starts"); } } class SportsCar : Car { // Cannot override Start here because it is sealed in Car }
Sample Program
This program shows a sealed class Fruit which cannot be inherited. It also shows a sealed method Grow in class Tree that cannot be overridden by Oak.
C Sharp (C#)
using System; sealed class Fruit { public void ShowType() { Console.WriteLine("This is a fruit."); } } // Uncommenting the following line will cause a compile error // class Apple : Fruit { } class Plant { public virtual void Grow() { Console.WriteLine("Plant grows"); } } class Tree : Plant { public sealed override void Grow() { Console.WriteLine("Tree grows tall"); } } class Oak : Tree { // Cannot override Grow() here because it is sealed in Tree } class Program { static void Main() { Fruit fruit = new Fruit(); fruit.ShowType(); Plant plant = new Plant(); plant.Grow(); Tree tree = new Tree(); tree.Grow(); Oak oak = new Oak(); oak.Grow(); } }
Important Notes
Sealed classes and methods help keep your code safe from unwanted changes.
Trying to inherit a sealed class or override a sealed method will cause a compile error.
Sealing methods can also improve performance slightly by avoiding virtual method calls.
Summary
Sealed classes cannot be inherited.
Sealed methods cannot be overridden further.
Use sealing to protect your code and improve clarity.
Practice
1. What does the
sealed keyword do when applied to a class in C#?easy
Solution
Step 1: Understand the sealed keyword on classes
Thesealedkeyword on a class means no other class can inherit from it.Step 2: Compare options with this meaning
Only Prevents the class from being inherited by other classes. correctly states that the class cannot be inherited.Final Answer:
Prevents the class from being inherited by other classes. -> Option AQuick Check:
Sealed class = no inheritance [OK]
Hint: Sealed class means no child classes allowed [OK]
Common Mistakes:
- Thinking sealed classes can be inherited
- Confusing sealed with abstract
- Assuming sealed allows overriding
2. Which of the following is the correct syntax to declare a sealed method in C#?
easy
Solution
Step 1: Recall sealed method syntax
A method can only be sealed if it overrides a base method, so it must haveoverride sealedmodifiers.Step 2: Check options for correct order and modifiers
public override sealed void MyMethod() { } correctly usespublic override sealed void MyMethod(). Other options missoverrideor have wrong order.Final Answer:
public override sealed void MyMethod() { } -> Option BQuick Check:
Sealed method = override + sealed [OK]
Hint: Sealed methods must override and use 'override sealed' [OK]
Common Mistakes:
- Declaring sealed method without override
- Wrong order of modifiers
- Missing override keyword
3. What will be the output of the following code?
class Base {
public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
public sealed override void Show() { Console.WriteLine("Derived"); }
}
class MoreDerived : Derived {
public override void Show() { Console.WriteLine("MoreDerived"); }
}
var obj = new MoreDerived();
obj.Show();medium
Solution
Step 1: Understand sealed override effect
The methodShowinDerivedis sealed, so it cannot be overridden inMoreDerived.Step 2: Check the code for override in MoreDerived
MoreDerivedtries to overrideShow, which causes a compilation error.Final Answer:
Compilation error -> Option DQuick Check:
Sealed method cannot be overridden [OK]
Hint: Sealed override blocks further overrides causing errors [OK]
Common Mistakes:
- Assuming MoreDerived.Show runs
- Ignoring sealed keyword effect
- Thinking output is Derived
4. Identify the error in this code snippet:
sealed class Animal {
public void Speak() { Console.WriteLine("Animal speaks"); }
}
class Dog : Animal {
public void Speak() { Console.WriteLine("Dog barks"); }
}medium
Solution
Step 1: Check sealed class inheritance rules
A sealed class cannot be inherited by any other class.Step 2: Analyze Dog class inheritance
Dogtries to inherit from sealedAnimal, which causes a compilation error.Final Answer:
Cannot inherit from sealed class Animal. -> Option CQuick Check:
Sealed class blocks inheritance [OK]
Hint: Sealed class cannot have child classes [OK]
Common Mistakes:
- Thinking method override causes error
- Ignoring sealed class inheritance rule
- Assuming code compiles fine
5. You have a base class
Vehicle with a virtual method Start(). You want to create a class Car that overrides Start() but prevents any further subclass from overriding it. How should you declare Start() in Car?hard
Solution
Step 1: Understand method sealing rules
To prevent further overrides, the method must be bothoverrideandsealed.Step 2: Check correct syntax for sealed override
The correct syntax ispublic override sealed void Start(). Other options have wrong order or missing keywords.Final Answer:
public override sealed void Start() { } -> Option AQuick Check:
Sealed override method syntax = override sealed [OK]
Hint: Use 'override sealed' to block further overrides [OK]
Common Mistakes:
- Omitting override keyword
- Wrong order of sealed and override
- Trying to seal without overriding
