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 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

  1. sealed class BankAccount โ†’ We sealed the class, so no one can inherit from it.
  2. ShowBalance() โ†’ A simple method to display balance.
  3. BankAccount myAccount = new BankAccount(); โ†’ We created an object and called the method.
  4. 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 as sealed, which means no other class can inherit from it.
  • When ChildClass tries to inherit ParentClass, 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! ๐ŸŽ‰

Leave a Comment

Share this Doc

Sealed

Or copy link