Sealed Inheritance in C# – Your Easy Guide with Examples
👋 Introduction
Hey buddy! Welcome back to your C# learning journey! 👨💻 Have you ever wondered how to stop a class from being inherited? Well, that’s exactly what Sealed Inheritance in C# is all about.
Imagine you designed a secure class that performs some sensitive operations, and you don’t want anyone to extend or modify that class. How would you protect it? The answer is – sealed keyword.
Let’s break it down step by step, nice and easy! 😊
📚 What You Are Going to Learn in This Lesson
✔️ What is Sealed Inheritance in C#?
✔️ Why is it important?
✔️ How to use the sealed
keyword
✔️ Sealing methods
✔️ Real-world examples
✔️ Best practices
🔑 What is Sealed Inheritance in C#?
In simple words, Sealed Inheritance in C# means stopping further inheritance of a class or method.
If a class is marked as sealed
, no other class can inherit from it. This helps to protect your code from unwanted changes.
🤔 Why is It Important?
👉 To prevent misuse of your class by other developers.
👉 To ensure security in sensitive applications.
👉 To improve performance (C# optimizes sealed classes).
👉 To maintain consistency in your code.
🔥 How to Use the Sealed Keyword?
Sealing a Class
You can seal a class by simply using the sealed keyword before the class name.
💻 Syntax
sealed class MyClass
{
public void Display()
{
Console.WriteLine("Hello from MyClass!");
}
}
class DerivedClass : MyClass // ❌ This will give an error
{
}
Output:
Compilation Error: Cannot inherit from sealed class 'MyClass'
See? The DerivedClass is trying to inherit from MyClass, but C# says – No way! 🔒
Sealing Methods
What if you only want to stop method overriding, not the whole class?
Here’s how you can seal a method inside an inherited class.
💻 Syntax
class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal makes sound");
}
}
class Dog : Animal
{
public sealed override void Sound()
{
Console.WriteLine("Dog barks");
}
}
class Puppy : Dog
{
// This will give an error because the Sound method is sealed
public override void Sound()
{
Console.WriteLine("Puppy barks");
}
}
Output:
Compilation Error: 'Puppy.Sound()' cannot override inherited member 'Dog.Sound()' because it is sealed
🌍 Real-World Example
Imagine you’re building an Online Payment System.- The PaymentGateway class handles the sensitive payment process.
- You don’t want anyone to inherit and change its behavior.
Example Code:
sealed class PaymentGateway
{
public void ProcessPayment()
{
Console.WriteLine("Payment processed securely");
}
}
class MyPayment : PaymentGateway // ❌ This will give an error
{
}
class Program
{
static void Main()
{
PaymentGateway pg = new PaymentGateway();
pg.ProcessPayment();
}
}
Output:
Payment processed securely
Now no one can modify your PaymentGateway class. Your payment system stays secure and reliable. 🔐
🟢 Best Practices
✅ Use sealed classes for security-sensitive operations.
✅ Use sealed methods to stop unnecessary method overriding.
✅ Don’t seal every class – only when necessary.
✅ Use sealed inheritance to improve performance.
🎯 Conclusion
In this lesson, you’ve learned:
✅ What Sealed Inheritance in C# is
✅ Why it is important
✅ How to use the sealed
keyword for both classes and methods
✅ How to secure your code with sealed inheritance
Sealed inheritance helps you protect your code and keeps your application safe from unwanted modifications. 🔒
⏭️ Next What?
You did amazing, my friend! 🎉 Now that you know about Sealed Inheritance in C#, it’s time to move on to another exciting topic – Polymorphism in C#!
Stay curious, keep coding, and I’ll see you in the next lesson! 🚀