Sealed Class in C#

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.

Syntax of Sealed


	public sealed class Xyz
{

}


		



Selaed Method

Sealed methods are used to prevent overridden method from further override. We cannot create sealed methods in base class.

Example of Sealed Methods


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


		Sealed

		Figure 1