PRESENTATION ON
OVERLOADING
Method Overloading 1tMyn
CONTENTS
Method Overloading 2tMyn
 Introduction to overloading
 Method overloading
 Example program for method overloading
Introduction to overloading
Method Overloading 3tMyn
  Overloading allows functions in computer
languages such as C++ and C# to have the same
function name but with different parameters.
 Overloading comes under static polymorphism.
 Static polymorphism can be achieved within the
class.
 The number of parameters and data type
of parameters must be different.It is showm
in the following example.
 Example:
Add()
Add(int i)
Add(int i,int j)
Add(int i,double j)
Add(double i,double j)
Method Overloading tMyn 4
Method overloading
 The process of creating more than one method in
a class with same name or creating a method in
derived class with same name as a method in
base class is called as method overloading.
In VB.net when you are overloading a method of
the base class in derived class, then you must use
the keyword “Overloads”.
But in C# no need to use any keyword while
overloading a method either in same class or in
derived class.
Method Overloading tMyn 5
While processing overloading methods, a
rule must be follow i.e, the overloaded
methods must differ either in number of
arguments they take or the data type of at
least one argument
Method overloading is commonly used to
create several methods with the same
name that perform the same or similar
tasks, but on different types or different
numbers of arguments.
6
EXAMPLE:
using System;
namespace ProgramCall
{
class Class1
{
public int Sum(int A, int B)
{
return A + B;
}
7
public float Sum(int A, float B)
{
return A + B;
}
}
class Class2 : Class1
{
public int Sum(int A, int B, int C)
{
return A + B + C;
}
}
l 8
class MainClass
{
static void Main()
{
Class2 obj = new Class2();
Console.WriteLine(obj.Sum(10, 20));
Console.WriteLine(obj.Sum(10,
15.70f));
Method Overloading tMyn 9
Console.WriteLine(obj.Sum(10, 20, 30));
Console.Read();
}
}
}
Output :
30
25.7
60
Method Overloading tMyn 10
The compiler distinguishes overloaded methods by their
signature :
 A combination of the method’s name ,data types and
order of its parameters.
 If the compiler looked only at method names during
compilation, the code in previous example would be
ambiguous.
 Overloaded method calls cannot be distinguished by
return type.
11
C# Method overloading

C# Method overloading

  • 1.
  • 2.
    CONTENTS Method Overloading 2tMyn Introduction to overloading  Method overloading  Example program for method overloading
  • 3.
    Introduction to overloading MethodOverloading 3tMyn   Overloading allows functions in computer languages such as C++ and C# to have the same function name but with different parameters.  Overloading comes under static polymorphism.  Static polymorphism can be achieved within the class.
  • 4.
     The numberof parameters and data type of parameters must be different.It is showm in the following example.  Example: Add() Add(int i) Add(int i,int j) Add(int i,double j) Add(double i,double j) Method Overloading tMyn 4
  • 5.
    Method overloading  Theprocess of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading. In VB.net when you are overloading a method of the base class in derived class, then you must use the keyword “Overloads”. But in C# no need to use any keyword while overloading a method either in same class or in derived class. Method Overloading tMyn 5
  • 6.
    While processing overloadingmethods, a rule must be follow i.e, the overloaded methods must differ either in number of arguments they take or the data type of at least one argument Method overloading is commonly used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments. 6
  • 7.
    EXAMPLE: using System; namespace ProgramCall { classClass1 { public int Sum(int A, int B) { return A + B; } 7
  • 8.
    public float Sum(intA, float B) { return A + B; } } class Class2 : Class1 { public int Sum(int A, int B, int C) { return A + B + C; } } l 8
  • 9.
    class MainClass { static voidMain() { Class2 obj = new Class2(); Console.WriteLine(obj.Sum(10, 20)); Console.WriteLine(obj.Sum(10, 15.70f)); Method Overloading tMyn 9
  • 10.
  • 11.
    The compiler distinguishesoverloaded methods by their signature :  A combination of the method’s name ,data types and order of its parameters.  If the compiler looked only at method names during compilation, the code in previous example would be ambiguous.  Overloaded method calls cannot be distinguished by return type. 11