«

»

Dec 24

method overloading and overriding in .net

Overloading vs Overriding

Conceptually overloading and overriding is way different. Only the notion about interface name is same in both cases.

Other than that, you cannot find a common thing between them.
Overloading is using the same interface but with different inputs and getting different behaviour as output.

Slightly confusing right. I hope by end of this article you may feel better.

Not Overloading

Overriding is in picture when there is inheritance. When you are inheriting a object there are so many behaviours associated with it. Primarily you are inheriting to use the common behaviour and attributes.

But in certain cases, you may not like a particular behaviour. In such cases, you overwrite that behaviour alone in your inherited instance. The interface will be same.
Overloading is not polymorphism. Run time binding is not polymorphism. All these and more are used to exercise the property polymorphism. Polymorphism is a more general property belonging to object-oriented

programming parlance and demands a separate article by itself. Let’s discuss that in detail then.

Overloading

Now let’s continue to discuss overloading and overriding. I have shown an image of a overloaded truck.

I specifically used this image and drawn a wrong mark on it. Because, when I did some research on this topic using internet almost all web pages where ever overloading is discussed this kind of overloaded truck image is used to explain the meaning. Please get it right. Overloading is not adding more and more attributes and interfaces to the object so that it looks bulkier. In fact, when you use overloading for the outsiders view the object will look compact.

That is putting more behavior with same interface. That is your object will look sleek.

Before Overloading

I have shown two images, one is Harmonium, a classical music instrument from India.

Just for understanding it is a very trimmed version of piano. The other is a modern digital keyboard. Harmonium is without overloading and keyboard is after overloading. In a digital keyboard, keys are the interface,in programming method name. Speaker is the output, in programming return type of the method. When the player presses the keys he gives input to the method and gets output as music through the speakers.

After Overloading

Following line is very important to understand the complete correlation. When different input is given you get different out put. Here the interface (method name, keys) is same and output type is same. But the actual input

(arguments, key press sequence) and output (returned data, music) is different. This is overloading.

Look at the whole object (keyboard), how sleek it is after overloading.
Example:

/Overloading
public class test
{
    public void getStuff(int id)
    {}
    public void getStuff(string name)
    {}
}

Overriding

It is very simple and easy to understand. When you inherit an object, you don’t like certain behaviour and so you replace it with your own. Note the word replace. Because after overriding the old behaviour is completely obsolete. Now look at the image of a monster truck. Here the old small four wheels are replaced with huge wheels to suit the current need. This is overriding.

Overriding
//Overriding
public class test
{
        public virtual getStuff(int id)
        {
            //Get stuff default location
        }
}

public class test2 : test
{
        public override getStuff(int id)
        {
            //base.getStuff(id);
            //or - Get stuff new location
        }
}
 
 

 overriding->

    1) method should be public.
    2)it need inheritance.
    3)it need virtual keyword before it declaration.
    4)it have same name with same parameter in different   class.
    5)it require non-static method.
    6)method should have same data type.

Overloading->

    1)method can be different access speicifier.
    2)it doesn't need inheritacne.
    3)all method should be in same class.
    4)method can have diffrent datatypes

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>