Monday, 7 April 2014

9: Method Overloading, Constructor Overloading in C#

Method Overloading

Consider
In object oriented programming each method has a signature.

What is signature?

The signature of the method is written in parenthesis next to the method name.
For example red text is the signature of the method.

public void Sum(int a, int b)
{
            Console.WriteLine("sum of two numbers are {0}",a+b);
}

Signature comprises of the number of the parameters passed to the method.

There is no rule in Object Oriented Programming that class is allowed to contain two methods with the same name and same signature.
What will happen when we have two methods with same name and same signature?


But it can be possible for a class to have two methods with same name but different signatures.
Methods overloading can be done when three condition always satisfying.
  1. When number of parameters change
  2. When order of parameters change
  3. When type of parameters change

Let’s talk about “When number of parameters change”

public void sum(int a, int b)
{
}
public void sum(int a, int b,int c)
{
}

In this case first method take two integer type parameters, second take three integer type parameters.

Let’s talk about “When order of parameters change”

public void sum(int a, string b)
{
}
public void sum(string a, int b)
{
}

In this case first method take two parameters one is integer and second is string, but in second method we have changed order of parameters.

Let’s talk about “When type of parameters change”

public void sum(int a, int b)
{
}
public void sum(string a, string b)
{
}

In this case first method take two parameters both are integer and second method we have two parameters both are string.

Example:

C# provide many overloaded methods in different classes
Like, WriteLine method of Console Class provides us 19 overloaded methods, you can use one of them according to requirement.



Constructor Overloading

The process of constructor overloading is similar to the method overloading, because each constructor has a signature like method.
Constructor overloading is used when different object of a class wants to initialized different variables.

Constructor overloading reduce the task of assigning different values to variables each time.

Example1:

class Laptop
{
string onOff;
public Laptop()
       {
            onOff = "Power Off";
       }
       public Laptop(string on)// overloaded constructor
       {
            onOff = on;
       }
       public string Status()
       {
            return onOff;
       }
}
class Program
{
static void Main(string[] args)
       {
            Laptop objLaptop1 = new Laptop();
            Console.WriteLine("Current Status" + objLaptop1.Status());
            Laptop objLaptop2 = new Laptop("Power On");
            Console.WriteLine("Current Status" + objLaptop2.Status());
            Console.ReadLine();
       }
      
}
Output:
Current Status Power Off
Current Status Power On

This example simply explains you that when you want to show Current Status Off you just use default constructor. Or if you want to show Current Status On just use overloaded constructor which taken string parameter for initializing onOff variable.

Another example

I have two methods, regular and special customer. Regular prints name and nicCard of customer and Special prints all information of customer.
When no overloaded constructor used, we have to initialize all variable of customer class in a constructor.

Class Customer
string name;
string fatherName;
string age;
string nicCard;
string passport;
string company;

Public Customer(string _name, string _fathername, string _age, string _nic, string _passport, string _company)
{
Name=_name;
.
.
.
Company=_company;
}
Public void regularCustomer()
{
// it print name and nicCard
}
Public void specialCustomer()
{
//  it print all information of customer
}

but when we use constructor overloading we resolve the issue of initializing all variables.

Example:
In this example overloaded constructors used, one is default constructor, second constructor initialize two variable and third constructor initialized six variables.

class Customer
{ 
string name;
string fatherName;
string age;
string nicCard;
string passport;
string company;

public Customer()
{
Console.WriteLine("Welcome Customer");
}
public Customer(string _name, string _nicCard)
{
name = _name;
nicCard = _nicCard;
}
      
public Customer(string _name, string _fathername, string _age, string _nicCard, string _passport, string _company)
{
name=_name;        
fatherName=_fathername;
age=_age;
nicCard=_nicCard;
passport=_passport;
company=_company;
}
      
public void regularCustomer()
{
Console.WriteLine("Welcome {0}\nYour NIC number is {1}",name,nicCard);
}
public void specialCustomer()
{
Console.WriteLine("Welcome {0}",name);
Console.WriteLine("Father Name: "+fatherName);
          Console.WriteLine("Age: "+age);
          Console.WriteLine("NIC Card Number: "+nicCard);
          Console.WriteLine("Passport Number: "+passport);
          Console.WriteLine("Company: "+company);}
}

As you can see there are three overloaded constructor it shows


When only need default constructor for welcome
use,
Customer objCustomer = new Customer();
Console.ReadLine();

Output:
Welcome Customer

When regular Customer information need to be show
use,
Customer objCustomer = new Customer("Waqas","0123456789");
objCustomer.regularCustomer();

Output:
Welcome Waqas
Your NIC number is 0123456789

When special customer information need to be show
use,
Customer objCustomer = new Customer("Ahmed","Aslam","20","0123456789","Pk12345","ABC");
objCustomer.regularCustomer();

Output:
Welcome Ahmed
Father Name: Aslam
Age: 20
NIC Card Number: 0123456789
Passport Number: Pk12345
Company: ABC


No comments:

Post a Comment