top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Different between method overriding and method overloading with real time examples in C#?

+4 votes
636 views
Different between method overriding and method overloading with real time examples in C#?
posted Mar 25, 2015 by Muskan

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

1 Answer

+1 vote
 
Best answer

What is Method Overloading ?

Creating a multiple methods in a class with same name but different parameters and types is called as method overloading.method overloading is the example of Compile time polymorphism which is done at compile time.

Method overloading can be achieved by using following things :

By changing the number of parameters used.
By changing the order of parameters.
By using different data types for the parameters.

Example:

public class Methodoveloading    
  {    
    public int add(int a, int b)  //two int type Parameters method  
    {    
        return a + b;       
    }    
    public int add(int a, int b,int c)  //three int type Parameters with same method same as above  
    {    
       return a + b+c;            
    }    
    public float add(float a, float b,float c,float d)  //four float type Parameters with same method same as above two method 
    {    
        return a + b+c+d;       
    }    
  }    

In the above example ,their are three methods with same method same but they differ with number of parameters and type of parameters ,hence this types of methods is called as method overloading.

What is Method overriding ?

Creating the method in a derived class with same name, same parameters and same return type as in base class is called as method overriding.
Method overriding is the example of run time polymorphism,how its is the part of run time polymorphism i will explain in detail.

Some Key Points of Method overriding

Method overriding is only possible in derived class not within the same class where the method is declared.
Only those methods are overrides in the derived class which is declared in the base class with the help of virtual keyword or abstract keyword.

Example:

public class Account
  {  
    public virtual int balance()  
    {  
        return 10;  
    }  
  }  
public class Amount:Account
{  

    public override int balance()  
    {  
        return 500;  
    }  
}  

Output of the above Program is

10 and 500

In the above small program their are two classes Account and Amount and both the classes contain same method Name that balance() in which Account class method returns 10 and Amount class returns 500 at run time because Account class method is overridden in a class Amount.
The Method overriding is very useful when we wants to return different output of same method in different class according to the need.
Let us consider the example I wants to provide the discount on particular product according to the Customer category that A,B,C in this scenario suppose A,B,C are the classes and Discount is the class which contains virtual CustDiscount () method ,then i simply override it on class A,B,C instead of writing three different methods for each class.

answer Mar 26, 2015 by Shivaranjini
...