How can you allow class to be inherited and prevent the method from being overridden?
- Declare the class as public and make the methods sealed.
- The method declaration with a sealed modifier is called as a sealed method.
- The same method must also include the override modifier.
- With the use of sealed modifier, a derived class is prevented from further overriding the method.
- The sealed modifier is used to prevent derivation from a class.
- An error occurs if a sealed class is specified as the base class of another class.
- A sealed class cannot be an abstract class.
- Sealed method enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.
Example:class A
{
protected virtual void show()
{
//Statements
}
}
class B : A
{
sealed protected override void shoe()
{
//Statements
}
}