I'm sure you've often come across usages of the seal keyword to used to stop people from inheriting from and changing the behavior of a given class. The .NET Framework includes several sealed classes they dont want us to extend - SqlConnection for example. But the seal keyword can also be used to seal a method (or a property for that matter), allowing us to seal only parts of the behavior of a class, while still allowing inheritance. Lets look at a silly example:
public abstract class BaseAction
{
public abstract void Execute();
}
public abstract class ConditionalAction : BaseAction
{
public sealed override void Execute()
{
if (Decide())
{
ExecuteTrueAction();
}
else
{
ExecuteFalseAction();
}
}
protected abstract bool Decide();
protected abstract void ExecuteTrueAction();
protected abstract void ExecuteFalseAction();
}
By sealing Execute() in ConditionalAction, we disallow overriding this method in classes that extend it. Instead, such classes must now implement Decide, ExecuteTrueAction and ExecuteFalseAction. Obviously, a class can still hide the implementation of Execute by using the new keyword, but that will not affect the behavior of any code that references instances of it as a ConditionalAction - remember that hiding is not the same as overriding.