Sealed classes and methods in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use sealed classes or methods, we stop further changes through inheritance. This affects how the program runs when it calls these methods.
We want to see how sealing changes the number of steps the program takes as it runs.
Analyze the time complexity of calling a sealed method versus a virtual method.
public class BaseClass
{
public virtual void VirtualMethod() { /* some work */ }
public sealed void SealedMethod() { /* some work */ }
}
public class DerivedClass : BaseClass
{
public override void VirtualMethod() { /* some work */ }
// Cannot override SealedMethod
}
// Usage
BaseClass obj = new DerivedClass();
obj.VirtualMethod();
obj.SealedMethod();
This code shows a base class with a virtual method and a sealed method, and a derived class overriding the virtual method but not the sealed one.
Look at what happens when methods are called many times.
- Primary operation: Calling methods on objects.
- How many times: Each method call happens once per call, but many calls can happen in a loop or program.
Calling a sealed method runs the exact code without extra checks. Calling a virtual method requires checking which version to run.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls, each with small overhead |
| 100 | 100 method calls, overhead adds up for virtual calls |
| 1000 | 1000 method calls, virtual calls have more overhead than sealed |
Pattern observation: Sealed methods avoid extra checks, so their call cost stays simpler as calls grow.
Time Complexity: O(n)
This means the total work grows directly with the number of method calls, but sealed methods have less overhead per call.
[X] Wrong: "Sealing a method changes how many times it runs or makes it faster in a way that changes overall time complexity."
[OK] Correct: Sealing only removes the option to override and avoids some checks, but the number of calls still grows with input size the same way.
Understanding how sealed methods affect execution helps you explain performance choices clearly. It shows you know how code structure impacts running time.
"What if the sealed method was changed to virtual and overridden many times? How would the time complexity or overhead change?"
Practice
sealed keyword do when applied to a class in C#?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]
- Thinking sealed classes can be inherited
- Confusing sealed with abstract
- Assuming sealed allows overriding
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]
- Declaring sealed method without override
- Wrong order of modifiers
- Missing override keyword
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();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]
- Assuming MoreDerived.Show runs
- Ignoring sealed keyword effect
- Thinking output is Derived
sealed class Animal {
public void Speak() { Console.WriteLine("Animal speaks"); }
}
class Dog : Animal {
public void Speak() { Console.WriteLine("Dog barks"); }
}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]
- Thinking method override causes error
- Ignoring sealed class inheritance rule
- Assuming code compiles fine
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?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]
- Omitting override keyword
- Wrong order of sealed and override
- Trying to seal without overriding
