C# Sealed Tutorial - Easy Guide with Real-World Example
Introduction โ What is Sealed in C#?
Imagine you just bought a brand-new mobile phone ๐ฑ. It works great, and you love it! But when you try to open it up and change the battery… guess what? Itโs sealed! ๐
Manufacturers donโt want you messing with the hardware, so they seal it.
In C#, we use the sealed keyword to do the same thing in code! When you seal a class, you prevent other classes from inheriting it. This ensures that no one can modify or extend its functionality, keeping it secure and stable.
What You Are Going to Learn in This Lesson
โ๏ธ What is the sealed keyword in C#?
โ๏ธ Why do we need it?
โ๏ธ A real-world example to make it crystal clear.
โ๏ธ A complete C# Sealed Example with code and output.
โ๏ธ Fun explanations so you never forget it! ๐
What is a Sealed Class in C#?
A sealed class is a class that cannot be inherited. Thatโs it! If you try to inherit it, BOOM! C# throws an error.
Real-World Example โ Bank Accounts! ๐ฆ
Think of a bank account system. The bank has a special class BankAccount that holds critical financial data.
The bank doesnโt want anyone to extend or modify this class (for security reasons). So, they seal it! That way, no unauthorized changes can be made.
C# Sealed Example
using System;
sealed class BankAccount // Sealing the class
{
public void ShowBalance()
{
Console.WriteLine("Your balance is $5000");
}
}
// Trying to inherit from a sealed class (This will cause an error!)
// class SavingsAccount : BankAccount { } // โ ERROR
class Program
{
static void Main()
{
BankAccount myAccount = new BankAccount();
myAccount.ShowBalance();
}
}
Output:
Your balance is $5000
Code Explanation
sealed class BankAccount
โ We sealed the class, so no one can inherit from it.ShowBalance()
โ A simple method to display balance.BankAccount myAccount = new BankAccount();
โ We created an object and called the method.- Trying to inherit from BankAccount โ It throws an error! ๐ซ
Why Do We Need Sealed Classes?
Hereโs why the sealed keyword is super useful:
๐ Security: No one can inherit and create a fake account.
๐ Performance: Sealed classes are optimized for performance.
โก Stability: Prevents unintended modifications in your system.
Example: Sealed Class Restricting Inheritance
using System;
sealed class ParentClass
{
public void Display()
{
Console.WriteLine("Hello from the sealed class!");
}
}
// โ ERROR: Cannot inherit from a sealed class
class ChildClass : ParentClass
{
public void ShowMessage()
{
Console.WriteLine("Trying to inherit from a sealed class.");
}
}
class Program
{
static void Main()
{
ParentClass obj = new ParentClass();
obj.Display();
}
}
If you try to compile this, you’ll see an error like this:
error CS0509: 'ChildClass': cannot derive from sealed type 'ParentClass'
๐ Why This Happens?
ParentClass
is marked assealed
, which means no other class can inherit from it.- When
ChildClass
tries to inheritParentClass
, C# immediately throws an error and stops compilation. - However, you can still create objects of a sealed class and use its methods, like we did in
Main()
.
Conclusion โ Why Sealed is Important?
The sealed keyword is like a lock on a bank vault. It prevents unauthorized modifications and inheritance, keeping your class safe and secure. It also improves performance because the compiler knows that the class won’t be extended.
Now you know why and when to use sealed classes in C#! ๐
Next What?
In the next lesson, youโll learn about the Dynamic Expression in C#! Itโs going to be fun, so stay tuned! ๐