Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Handy Tricks and Tips to do your .NET code Fast, Efficient and Simple. Some common questions that comes into mind. Please check if you could find them listed or not.
Browse by Tags · View All
CodeProject 36
C# 36
.NET 28
beyondrelational 20
internals 19
.NET 4.0 17
BRH 15
.NET 3.5 13
WPF 11
architecture 11

Archive · View All
April 2011 6
July 2011 5
January 2011 5
February 2011 5
March 2011 5
June 2011 4
September 2011 3
May 2011 2
February 2012 1
August 2011 1

Abhishek Sur's Blog

Concept Overide vs Method Hiding in terms of C#

Jun 24 2011 7:36PM by Abhishek Sur   

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.


Republished from DOT NET TRICKS [47 clicks].  Read the original version here [32134 clicks].

Abhishek Sur
121 · 1% · 426
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]