Method overloading (or Overloading methods)

by Jagadish Pulakhandam on 12/10/2011 10:14:57 AM
Rated 0 from 0 votes
Brief: Demonstrates an example which explains the concept of "Overloading Methods" in OOPS
Posted to: Basics of Object Oriented Programming in .NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Overloading Methods - Methods which have the same name in the same class but with difference in parameters.  The difference of parameters is considered against data types, their order and count (but not against parameter names or return types).

Following code demonstrates the following:
  • How to define multiple methods with the same name (but with difference in parameters)
  • Defines three methods with the same name.  But, no two methods will have same type/count/order of parameters.
Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace MethodOverloading
07.{
08.    class Employee
09.    {
10.        public int Empno;
11.        public string Ename;
12.        public double Sal;
13. 
14.        public void SetValues(string Name, double Salary)
15.        {
16.            this.Ename = Name; //"this" means current instance (or object) from where this method was called for execution
17.            this.Sal = Salary;
18.        }
19. 
20.        public void SetValues(int empno, double Salary)
21.        {
22.            this.Empno = empno;
23.            this.Sal = Salary;
24.        }
25. 
26.        public void SetValues(int empno, double Salary, string Name)
27.        {
28.            this.Ename = Name;
29.            this.Empno = empno;
30.            this.Sal = Salary;
31.        }
32. 
33.    }
34.}

01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09. 
10.namespace MethodOverloading
11.{
12.    public partial class Form1 : Form
13.    {
14.        public Form1()
15.        {
16.            InitializeComponent();
17.        }
18. 
19.        private void button1_Click(object sender, EventArgs e)
20.        {
21.            Employee e1 = new Employee();
22.            e1.SetValues("Jag", 4000.0); //picks first "SetValues" method as we are sending string and double values
23. 
24.            Employee e2 = new Employee();
25.            e2.SetValues(1001, 4000.0); //picks second "SetValues" method as we are sending int and double values
26. 
27.            Employee e3 = new Employee();
28.            e3.SetValues(1001, 4000.0, "Jag"); //picks third "SetValues" method as we are sending int, double and string values
29. 
30.        }
31.    }
32.}


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following