What is Method Overriding?

Method Overriding is a process by which the abstract methods that are declared in the abstract class are implemented in the child class, that is the class where the abstract class is inherited.

The method should be preceded by the keyword "override" or "virtual" and should be having the same signature, that is the same return type, same number of parameters and type.

The virtual methods does have an implementation which can be overridden in the sub class where as an abstract method must be implemented in the sub class.


For e.g.

public class A
{
     public virtual double Area (double r)
     {
          return r * r;
     }

}

public class B : A
{
     public override double Area (double r)
    {
        double p = 3.142;
        return base.Area(r) * p;
    } 

}


abstract class MyAbs
{
    public abstract void AbMethod(); // An abstract method
}

public class MyClass : MyAbs
{
    public override void AbMethod()
    {
        Console.WriteLine("Abstarct method");
    }
}

Comments

Popular posts from this blog

jQuery Basics

What is the difference between a Page Layout and Master Page in SharePoint?

Accessing data from SharePoint 2010 to an ASP.NET application