Wednesday, October 5, 2011

How to override the method in Polymorphism


How to override the method –

                If we want to override a method first under the parent class the method should be declared using virtual modifier, now the child class gets the permission to override the method which can be overridden by the child class using override modifier, but overriding the method is only optional for the child class.
eg – LoadParent
public virtual void test1(int x)
        LoadChild : LoadParent
public override void test1(int x)

Overriding is an option which allows to change the behavior of the parent class method under child class according to its requirement. To change the behavior parent class is giving the permission by declaring the method as virtual.

- To test this goes into the Class LoadParent and rewrite the Test method as following 
        public virtual void test1(int x)
        {
        Console.WriteLine("method test1");
        }

Now run the child class and check the result which also prove overriding is only optional.
- Add a new method under the class LoadChild as following
        public override void test1(int x)
        {
            Console.WriteLine("Method test 3");
        }

Now again run the child class and check the difference in result , where in the place of virtual method the overridden method gets executed, Because object of the child class gives First preference to Local methods .

After the child class is overriding a virtual method of its parent if required the child class has a chance to invoke virtual methods of Parent Class also, which can be done in two different ways –
  • By creating the object of parent class child class can invoke Parent class’s virtual method.

- To test this rewrites the code under main method of class LoadChild as following –
{
            LoadParent p = new LoadParent();
            LoadChild c = new LoadChild();
            p.test1(10);           // Invoke parent class method
            c.test1(10);           //Invoke child class method
            Console.ReadLine();
        }

Note – Earlier we have discussed references of parent class that are created using object of child class cannot invoke child class members but we have an assumption in overriding i.e. these references can invoke the overridden methods under child class because overriding is performed with the permission of parent.

To test this rewrites the code under main method of class LoadChild as following –
        {
           
            LoadChild c = new LoadChild();
            LoadParent p = c;      
            p.test1(10);           // Invokes child class method only
            c.test1(10);           //Invokes child class method
            Console.ReadLine();
        }

.       using base ‘keyword also from child class from child class we can invoke virtual methods of parent but using of “base and this key words” from a static block is invalid
To test the process add new method under child class as following –
        public void Ptest(int x)
        {
            base.test1(x);
        }

Now in the main method of Child class rewrite the code as following –
       {
           
            LoadChild c = new LoadChild();
            c.Ptest(10);            // Invokes Parent class method
            c.test1(10);           //Invokes child class method
            Console.ReadLine();
        }

Q. How to re-define a parent class method under child class –
Ans – A parent class method can be re- defined under its child class using two different approaches
  • Method overriding
  • Method Hiding
  1. In the first case parent class gives the permission for child class to re-define its method by declaring it as virtual, now he child class can re-define the method using override modifier.
  2. In the second case parent class doesn’t give any permission to re-define its method to the child class, but still the child class can re-define it without any permission of its parent.

LoadParent

public void show()
public virtual void test (int x)

LoadChild : LoadParent

public    new   void  show()               // method hiding
public    override   void   test (int x)   // method overriding
To test this add new method under the class load child as following-
        public new void show ()
        {
            Console.WriteLine("Method show 2");
        }

All the rules we have discussed in case of overriding applies to method hiding also accept reference of parent class created from object of Child can invoke overridden methods of child but not the methods which are re-implemented using method hiding approach.
To check this rewrite the code under main method of class load child as following-
        {
           
            LoadChild c = new LoadChild();
            LoadParent p = c;      
            p.show();        // Invokes parent method because this is method hiding
            c.test(10);     //Invokes child method because this is method overriding
            Console.ReadLine();
        }

Polymorphism is of two types
  1. Static or compile time polymorphism.
  2. Dynamic or run time polymorphism.

In the first case the object of class recognizes which Polymorphic method it has to call at the time of compilation of the program only, which is possible in overloading.
In the second case the object of a class recognizes which Polymorphic method it has to call at run time of while executing the program, this happen in case of method overriding and method hiding.

No comments: