25.6.13

Straightforward points about Overriding


Overriding comes into use when we have a requirement in which we want to keep the same name and same signature for more than one method. Let's know more about overriding in more simple words with a simple example of how it could be achieve..

Overriding also known as Run-time polymorphism or dynamic polymorphism. This is so because C# compiler doesn't have any information about the method to be execute at the compile time.

It is all determined when the type of object compiler see at run-time, i.e. after clearing out whether it has any syntactical error.

When you are about to override any method in a class hierarchy you must define this method as 'virtual' as follows


public virtual methodType methodName(parameterType parameterName);
or
virtual public methodType methodName(parameterType parameterName);


When you want to override this method  just replace the keyword 'virtual' with 'override'.


public override methodType methodName(parameterType parameterName);


Lets see, how to tell compiler which version of method should be executed at run-time.

Just create an object reference of Base class and then pass the objectReference of that class
that you want to override the method defined in base class.

We check this in the following example.
namespace Override_
{
    class Over_ride
    {
        virtual public void method1()
        {
            Console.WriteLine("I am the mehod1 of Over_ride class\n");
        }
    }

    class B:Over_ride
    {
        public override void method1()
        {
            Console.WriteLine("I am the method1 of B class.\n");
        }
    }

    class C : Over_ride
    {
        public override void method1()
        {
            Console.WriteLine("I am the method1 of C class.\n");
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Over_ride workerObject; //reference variable
            Over_ride baseObe = new Over_ride();

            workerObject = baseObe;
            workerObject.method1();
            B bClassObj = new B();
            workerObject = bClassObj;
            workerObject.method1();
            C cClassObj= new C();
            workerObject = cClassObj;
            workerObject.method1();            
        }
    }
}

Output of this program would be as

I am the method1 of Over_ride class
I am the method1 of B class
I am the method1 of C class
press any key to continue. . .


Well what if you don't write override keyword before the method that you want to override.
...
 class Over_ride
    {
        virtual public void method1()
        {
            Console.WriteLine("I am the mehod1 of Over_ride class\n");
        }
    }

    class B:Over_ride
    {
        public void method1()
        {
            Console.WriteLine("I am the method1 of B class.\n");
        }
    }
...

If you didn't precede the keyword override before method to be override in child /derived class, it would not result in any run-time error but it will be a logical error and this child class shall not be able to override the version of base class and in the output you will see that version; that you have defined in base class. You can see the output of above program as follows:


I am the method1 of Over_ride class
I am the method1 of Over_ride class
I am the method1 of C class
press any key to continue. . .


so friend, it was all about overriding in c#

This will help you in creating a generalize to specialize structure of any class hierarchy. For example Family tree of a family.for instance every man go for work, you can create a Work method in which you could depict the profession of grand father is 'x', profession of  father/mother is 'y',  profession of kid(s) is 'study'. etc. 

Thank you!!




[Note:

* You all are welcome to express your views as comment in the box given below of this post. 

* I have not made any special conditions to write here your comment but you may write whatever your heart speak out loud in your inner-self.

* This blog space is continuously in writing/edit mode, kindly keep visiting for a full version]


Post related to OOPs/C# programming /Visual Studio

No comments:

Post a Comment

Search This Blog

Let make your friend aware about this post