When a class is marked as sealed, it means that it cannot be used as a base class for further inheritance.
Similarly, when a method is marked as sealed, it means that it cannot be overridden in derived classes.
Sealed Class:
public sealed class FinalClass
{
// Class members and methods
}
// This will cause a compile-time error
public class DerivedClass : FinalClass
{
// Cannot inherit from a sealed class
}
Sealed Method:
public class BaseClass
{
public virtual void Method1()
{
Console.WriteLine("Method1 in BaseClass");
}
public sealed void Method2()
{
Console.WriteLine("Method2 in BaseClass");
}
}
public class DerivedClass : BaseClass
{
// This will cause a compile-time error
public override void Method1()
{
Console.WriteLine("Method1 in DerivedClass");
}
}