Pages

Saturday, June 25, 2011

Concept Overide vs Method Hiding in terms of C#

Overriding is one of the most interesting topic that many software professionals are dealing with quite regularly. Most of the applications we write in .NET or other languages somehow uses overriding. While you write your class, one of the most important thing that you need to consider is overriding ToString() to ensure that your class does not produce the “Name of the class” rather you produce something useful on your context. Method hiding is another concept similar to Overriding but is actually different in many respect. But I saw many people does have a little confusion among these two concepts and does not know when to use what. Here is some sample which would help you deal with these concepts easy.

Overriding

Overriding means redefining an existing method that comes inherited from parent class. Say for instance, you have defined a class Animal. Animal can move and eat. So let’s define the class :


public class Animal
    {
        public virtual void Walk()
        {
            Console.WriteLine("Animal is now Walking");
        }
        public void Eat()
        {
            Console.WriteLine("Animal eats for living.");
        }
    }


So this is quite a simple class that provides two methods. The first one is Walk which prints a message, and another is Eat, which again prints another message. Now notice, I have made the Walk method of Animal Virtual. Virtual means, the method will be bound at runtime, hence no compile time binding will be produced and runtime object will take over compile time bindings.



Now let’s define another class Dog which is derived from Animal.

public class Dog : Animal
    {
        public void Bite()
        {
            Console.WriteLine("Dogs can bite");
        }

        public override void Walk()
        {
            Console.WriteLine("Dog walks in four legs");
            base.Walk();
        }
    }


So here the class Dog actually overrides the method Walk and writes another message. base keyword is used to point to base class method.

Now let’s try the two classes


So basically calling d.Walk will actually call the method that I have defined for Dog class. You can see when I create the object of Dog, it can also call the Walk method defined in Animal class which is inherited to it using base keyword. Hence you can say both method co-exists side by side.

Now let’s change the call a little :

So here I just changed the Reference type to Animal from Dog. But it seems to have no effect on output. It can still call it. So :


  1. Override for virtual method determine its link at runtime based on actual object rather than the Type it specifies. (as here reference of Animal actually calls Walk of Dog as we pass object of Dog to it)
  2. For every object both set of inherited members and its own overridden methods co-exist, where the base method can be accessed using base keyword.
  3. Override keyword is used to override a virtual method.

Method Hiding


Method Hiding is a concept new to C# which hides the inherited member of a class with a new one. In case of Method hiding the runtime linking does not occur, hence compile time Links are important while both the method co-exists in the object.

Let’s try to override the method Eat in Dog.

public class Dog : Animal
    {
        public void Bite()
        {
            Console.WriteLine("Dogs can bite");
        }

        public override void Walk()
        {
            Console.WriteLine("Dog walks in four legs");
            base.Walk();
        }

        public new void Eat()
        {
            Console.WriteLine("Dog eats meat");
            base.Eat();
        }
    }

Here the Eat method coming from base class Animal is hidden using a new keyword. This is called method Hiding. Notice, you can still access Eat of Animal using “base” keyword. Then what is the difference? Let’s try:

So if I call Eat method from Dog object, it will call the Method Eat from class Dog which in turn can call the Animal. Hence the output in this is same as override.

On the other hand,

Here you can see, the Runtime object cannot call the method Eat of Dog, even though we pass the object of Dog on Reference Animal. Hence the Compile time binding cannot be overridden in case of Method Hiding.

In terms of IL



Yes, IL is no different for the two cases. Callvirt is actually used to produce a late bound call to a method, but as I said Method hiding cannot override the Compile time bindings, it will call Eat method of Animal that is written during compilation rather than override, which calls Walk appropriately with because of virtual method.

I hope this post comes handy.

Thanks for reading.

No comments:

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.