Monday, April 25, 2016

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");
    }
}

No comments:

Post a Comment