StatCounter

View My Stats

Tuesday, February 10, 2009

Use of Sealed Keyword (or) Sealed Method

Use of Sealed Keyword (or) Sealed Method


The sealed method cannot be override at the derived class. it is mainly associated with the
override method.

For the below example at the DerivedSealed1 class the override function Base() associated with the "sealed" keyword.So it can't able to override the next level of inheritance.
For the below example the error thrown at the "DerivedSealed2 " override Base() Function.

If DerivedSealed1 the Base() override is not associated with the "sealed" keyword this wont thrown the error.

I hope that u can understand the use of "sealed Keyword"

public class BaseSealed
{
public virtual void Base()
{
Console.WriteLine("I am in Base Class");
}

}
public class DerivedSealed1 : BaseSealed
{
public sealed override void Base()
{
Console.WriteLine("I am in Derived Class 1");
}

}
public class DerivedSealed2 : DerivedSealed1
{
public override void Base()
{
Console.WriteLine("I am in Derived Class 2");
}

}

No comments: