C# > Object-Oriented Programming (OOP) > Inheritance > Sealed Classes and Methods
Sealed Method Example
This example demonstrates how to define and use sealed methods in C#. Sealed methods prevent derived classes from overriding a virtual method, ensuring a specific implementation is always used down the inheritance hierarchy.
Base Class with Virtual Method
BaseClass
defines a virtual method DisplayMessage
. Derived classes can override this method to provide their own implementation.
public class BaseClass
{
public virtual void DisplayMessage()
{
Console.WriteLine("Base Class Message");
}
}
Derived Class with Override
DerivedClass
overrides the DisplayMessage
method, providing a different implementation. Now, if you create an instance of DerivedClass
and call DisplayMessage
, the Derived class implementation will execute.
public class DerivedClass : BaseClass
{
public override void DisplayMessage()
{
Console.WriteLine("Derived Class Message");
}
}
Sealed Method in Another Derived Class
AnotherDerivedClass
overrides DisplayMessage
and seals it. This means that any class inheriting from AnotherDerivedClass
*cannot* override DisplayMessage
. The keyword sealed
is used in conjunction with the override
keyword.
public class AnotherDerivedClass : DerivedClass
{
public sealed override void DisplayMessage()
{
Console.WriteLine("Another Derived Class Message");
}
}
Attempting to Override Sealed Method (Compile-Time Error)
If you uncomment the code, you'll find that it will not compile due to the fact that you are attempting to override a sealed method. The sealed method prevent further method overriding.
// This code will cause a compile-time error.
// public class FinalDerivedClass : AnotherDerivedClass
// {
// public override void DisplayMessage()
// {
// Console.WriteLine("Final Derived Class Message");
// }
// }
Usage Example
This code demonstrates how each class's DisplayMessage
method is called, showcasing the effect of the override and sealed keywords.
public class Example
{
public static void Main(string[] args)
{
BaseClass baseObject = new BaseClass();
DerivedClass derivedObject = new DerivedClass();
AnotherDerivedClass anotherDerivedObject = new AnotherDerivedClass();
baseObject.DisplayMessage(); // Output: Base Class Message
derivedObject.DisplayMessage(); // Output: Derived Class Message
anotherDerivedObject.DisplayMessage(); // Output: Another Derived Class Message
}
}
Concepts behind the snippet
Sealed methods provide a means to prevent subclasses from overriding a specific virtual method, thus preserving the exact behavior of that method in the inheritance chain.
Real-Life Use Case
Suppose you have a method that performs a critical calculation. You might seal it to ensure that derived classes don't inadvertently (or maliciously) alter the calculation logic.
Best Practices
Seal methods cautiously. If there's a reasonable possibility that a derived class might need to customize the behavior, avoid sealing the method. Seal only when you're confident that the current implementation is definitive.
When to use them
pros
cons
FAQ
-
Can I seal a non-virtual method?
No, you can only seal methods that are already virtual. Thesealed
keyword is used in conjunction with theoverride
keyword. -
What happens if I try to override a sealed method?
You will get a compile-time error. The compiler will prevent you from overriding a sealed method.