Sealed keyword is used to prevent a class from being inherited. It means if we declare class as a sealed class then we cannot further inherit it.
public sealed class Xyz
{
}
Sealed methods are used to prevent overridden method from further override. We cannot create sealed methods in base class.
public class Abc
{
public virtual int sum(int x, int y)
{
return (x + y);
}
}
public class Pqr : Abc
{
public sealed override int sum(int x, int y)
{
return (x + y) * 2;
}
}
public class Mcq : Pqr
{
public override int sum(int x, int y)
{
return (x + y) * 3;
}
}
In the above example in Abc class we have declared sum as virtual method and override it in Pqr class. But we also sealed it in Pqr. If we try to override it in Mcq class, it will throw following error:-
Figure 1