Method Overridding in C#


Introduction



Before start "Method Overriding" lets understand Lets understand What is Polymorphism is.......
 Polymorphism is one of the most important pillar of OOPs in C#.  
Polymorphism is of 2 types.
  • Compile time Polymorphism
  • Run time Polymorphism
Lets Get Started....

See the below image(Fig-1).

Fig-1









Compile time polymorphism is called Method overloading where as Run time polymorphism is called Method Overriding.

Lets Get Started

As the name suggest it override the method. 
Lets take an example.



class ParentClass
    {
        public virtual void addition()
        {
            Console.WriteLine("parent");
        }
    }

    class childclass : ParentClass
    {
        public override void addition()
        {
            Console.WriteLine("child");
        }        
    }

class Program
    {
             public static void Main(string[] args)
        {
            childclass cp = new childclass();
            cp.addition();        //child method calld
            ParentClass pc = new ParentClass();
            pc.addition();        //parent method calld
            pc = cp;                //Reference variable of child class pointing to parent class object
            pc.addition();      //child method called
            Console.ReadKey();
        }
}




In the above code you can see We have 2 class. ''Parentclass''  and "ChildClass" . As we declare Virtual to parent class method and Override in child class method,  it can Override the Parentclass method.  when we try to access the method "addition()" then child method get called.

But if we want Parentclass method to be called then we can call the method by creating the object of the parent class.

What happen when the reference variable of child class pointing to the parent class object????It will call to the childclass method as shown in the above code. This is the specialty of method overridding.

Output











Method Overriding can be applied if  a method contains the keyword Virtual,abstract,Override.

In the above example we have seen how we used Virtual Keyword.Lets see an example where we can apply abstract and override to override the parent class.


abstract class absParentClass
    {
      public abstract void substraction();
        public virtual void addition()
        {
            Console.WriteLine("parent method called");
        }
    }
    class Childclass : absParentClass
    {
        public override void addition()
        {
            Console.WriteLine("child method called");
        }

        public override void substraction()
        {
            Console.WriteLine("abstract method called");
        }
    }
    class grandchildlass : Childclass
    {
        public override void addition()
        {
            Console.WriteLine("grand child addition");
        }
        public override void substraction()
        {
            Console.WriteLine("grand child");
        }
    }


See the above code. We can declare abstract keyword to the method in abstract class. The abstract method can be declared, can not defined. So we can define the method in child class by using the keyword "override" as shown above.

Now we understood the abstract keyword. But lets see an interesting thing related to Override....

Can we use "override" keyword to a parent class and override the same method in child class?

Of course. Lets see the above code.

Childclass and Grandchildclass having the methods and both use override keyword. Here in example Grandchildclass tried to access the childclass method using override keyword.

Here Just remember that we can not use static,virtual and new to modify the override keyword.


Few things need to be remember
  • we can declare virtual,override,abstract keyword in parent class methods.
  • We can only declare override in child class method.
  • Object of child class pointing to the reference variable of parent class call the child class method.


Thanks.......................










Share:

No comments:

Post a Comment

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts