Method Overloading

Each method has a signature. The signature comprises the method's name and its parameters (excepting their names), but not the method's return type. Method Overloading is one of the forms of polymorphism. This is compile time polymorphism because at compile time the compiler able to distinguish between the methods that is declared with same name but different number of arguments.



Conditions which should be fulfilled for Method overloading:


* Method Name should be same for all the methods that are participating in method Overloading.

* The number of arguments or type of arguments should be different.

* No class allowed two methods to have same signature.


Note:- Like methods constructors can also be overloaded.




ClsOverLoading19.cs



using System;

using System.Collections.Generic;

using System.Text;



namespace cSHARPEXAMPLES

{

class ClsOverLoading19

{

int a;

int b;

//Constructor OverLoading

public ClsOverLoading19() { a = 10; b = 20; }

public ClsOverLoading19( int a, int b) { this .a = a; this .b = b;}

public ClsOverLoading19( ClsOverLoading19 obj) { this .a = obj.a;

this .b = obj.b; }

public int result() { return a + b; }



//Method OverLoading

public string display() { return "single method with no parameter" ; }

public string display( string strA) { return strA; }

public string display( string strA, string strB) { return strA + ":: " + strB ; }

public string display( string strA, int intA) { return strA + " :: " + intA.ToString() ; }

}

}



Form19.cs



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



namespace cSHARPEXAMPLES

{

public partial class Form19 : Form

{

public Form19()

{

InitializeComponent();

}

private void button1_Click( object sender, EventArgs e)

{ // Call Single Parameter Method

ClsOverLoading19 obj = new ClsOverLoading19 ();

MessageBox .Show(obj.display());

}

private void button2_Click( object sender, EventArgs e)

{ //Call Parameterized Method

ClsOverLoading19 obj = new ClsOverLoading19 ();

MessageBox .Show(obj.display( "vikrant" ));

MessageBox .Show(obj.display( "Vikrant" , "Dogra" ));

MessageBox .Show(obj.display( "Hi I am " ,27));

}

private void button3_Click( object sender, EventArgs e)

{ //Constructor overloading

ClsOverLoading19 obj = new ClsOverL oading19 ();

ClsOverLoading19 obj1 = new ClsOverLoading19 (30,40);

ClsOverLoading19 obj2 = obj1;

MessageBox .Show( "Default Constructor result " + obj.result());

MessageBox .Show( "Parameterized Constructor result " + obj1.result());

MessageBox .Show( "Copy Constructor result " + obj2.result());

}

}

}


Comments

Guest Author: Misa02 May 2012

I know these concept from JavaScript. But I Worden for what this is good to? For me its looks like an alternative Syntax. The hello() example is the same as you wrote it like:public function hello() { }The feature of closures makes only sense, when you can pass them into a function as parameter and when you can return it as a parameter and finally when you can execute the closure with an different object context.Can I do in PHP the following things?// returning a closure:public function foo() { return function() { };}// passing closure around (e.g. for anonymous callbacks):$this->bar($param, function() use ($foo) { });public function bar($p, $callback) { // $callback($someValueFromBar);}// can I invoke the closure with different object contexts ($this):public function bar($p, $callback) { $someOtherObject = new ; // $callback->applay($someOtherObject);}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: