top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Explain Method Overloading and Overriding in ASP.NET with Example?

0 votes
1,243 views
Explain Method Overloading and Overriding in ASP.NET with Example?
posted Feb 16, 2016 by Sathyasree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Method or Function Overloading

ASP.NET has lots of classes which provide several methods that offer overloaded methods. In fact, you will see method overloading all over the place in ASP.NET classes. Method or Function Overloading is a programming language feature that is used in different languages including ASP.NET languages. Method Overloading means creating and using two or more methods with same name but different inputs or number of parameters. It means there can be several versions of the same method with variation in input and number of parameters.

ASP.NET provides IntelliSense to see different overloaded versions of functions. The appropriate function is called according to given parameters. Compiler figures it out that which overloaded method should be called with given parameters. It will throw an error if appropriate input is not given or number of parameters provided is not matched with any overloaded version of the called method. Overloaded methods can be in the same class or in base and derived classes.

Simple Overloading method in ASP.NET:

C#

string a = "String Value";
int b = 34;

Console.Write(a);
Console.Write(b);

VB.NET

Dim a As String = "String Value"
Dim b As Integer = 34

Console.Write(a)
Console.Write(b)

Write() method of Console class has overloaded versions. It can take string, integer, decimal or char value. Same is the case with WriteLine() method of Console class.

We can write our own overloaded methods like methods given in ASP.NET

C#

public int Add(int x, int y)
{
    return x + y;
}
public int Add(int x, int y, int z)
{
    return x + y + z;
}

VB.NET

Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
    Return x + y
End Function
Public Function Add(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
    Return x + y + z
End Function

I have created two functions with same name but different number of parameters. First Add() function takes two arguments and second takes three. Now we can call overloaded function as below.

C#

Add(1, 2);
Add(1, 2, 3);

VB.NET

Add(1, 2)
Add(1, 2, 3)

Compiler will find that first call is for first version of the Add() function and second call is for second overloaded Add() function with three parameters.

Method or Function Overriding

Method or Function Overriding is also a programming feature provided in many programming languages. Function Overriding means a method created in derived class with same name, same number of parameters and same return type as created in base class. This is in fact a new version of the same method in derived class which has different implementation than a same method in base class.

Overridden method cannot be in the same class. A method created in base class can be overridden in derived class for different implementation. Method overriding is needed when we need to perform different implementation of the same method in derived class.

We can do Function Overriding like below.

C#

public class A
{
    public virtual void showMessage()
    {
        Console.WriteLine("class A");
    }

}
public class B : A
{
    public override void showMessage()
    {
        Console.WriteLine("class B");
    }
}

VB.NET

Public Class A
    Public Overridable Sub showMessage()
        Console.WriteLine("class A")
    End Sub

End Class
Public Class B
    Inherits A
    Public Overrides Sub showMessage()
        Console.WriteLine("class B")
    End Sub
End Class

In C#, we have to write keyword “virtual” before the method in base class that we want to override in inherited class and we have to write “override” keyword before overridden method in derived class. In VB.NET “Overridable” keyword is used in place of “virtual” and “Overrides” keyword is used for “Override”.

We can call this method as below

C#

A obj1 = new A();
obj1.showMessage();

B obj2 = new B();
obj2.showMessage();

VB.NET

Dim obj1 As New A()
obj1.showMessage()

Dim obj2 As New B()
obj2.showMessage()

Result will be as below.
class A
class B

answer Feb 17, 2016 by Shivaranjini
...