Pages

Wednesday, May 15, 2013

Sealed Methods in c#

By using sealed method ,we can stop the bahaviour overloading.
when u want to stop bahavour change then declare your method as sealed method
-> You can declare only one override method.
->At a time we can not declare "sealed virtual".
->we can apply sealed keyword only on the override method(MIIQ)

Sample code:

public partial class sealed_class3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        C c1 = new C();
        int r1 = c1.m1(10, 20);
        Response.Write(r1);

    }
}
public class A
{
    public virtual int m1(int x, int y)
    {
        return x + y;
    }
}
public class B : A
{
    public override int m1(int x, int y)
    {
        return x - y;
    }
}
public class C : B
{
    public sealed override int m1(int x,int y)
    {
        return x * (x + y);
    }
} 
 public class D : C  //if u try like this it will be not work ,bec after sealed method no metod will be work
{
  public override int m(int x, int y)
   {
    return x * (x - y);
   }
}

No comments:

Post a Comment