C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Compile Time Polymorphism -- Function Overloading


Posted Date: 20 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     Points: 12



Polymorphism means one thing exists in many forms of its own with each form having different implementation of its own.
Polymorphism can be compile time or runtime.

Compile time polymorphism includes:-
1.) Function Overloading
2.) Operator Overloading

Function Overloading:-
Function overloading depends on:-
1.) Variable Number of parameters to function
2.) Order of parameters to function
3.) Data type of parameters
Function overloading doesn’t depend on:-
1.) Return type of function
2.) Name of parameters

We will consider various scenarios for function overloading in the next Code samples.
Let’s first consider cases on which function overloading does not depend on:-

1. Return Type of Function


public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public int foo()
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}


The code sample will generate the following error:-

Type ‘MyBaseClass' already defines a member called 'foo' with the same parameter types

2. Name of parameters

    
public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("Hi I am original");
}
public int foo(int j)
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}


The above code will give the following error:-
Type 'MyBaseClass' already defines a member called 'foo' with the same parameter types.

Now we will consider cases on which function overloading depends on:-
3. Variable Number of parameters to function


public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public void foo(int i)
{
Console.WriteLine("Hi I am overloaded and here is the value of i " + i.ToString());
}
}


In this example, number of parameters are different so this will compile fine and now when you create object of this class you will see two overloaded version of foo.
Lets now create object of this class and call both the versions:-


MyBaseClass obj = new MyBaseClass();
obj.foo();
obj.foo(10);


Here is the output:-

Hi I am original
Hi I am overloaded and here is the value of i 10


4. Order of parameters to function


public class MyBaseClass
{
public void foo(int i , string s)
{
Console.WriteLine("The parameter order is int and string.The value of i is {0} and that of s is {1}",i,s);
}
public void foo(string s, int i)
{
Console.WriteLine("The parameter order is string and int.The value of i is {0} and that of s is {1}", i, s);
}
}


In the example above, number of parameters are same but since the order is different so this will compile fine.

Lets now create object of this class and call both the versions:-


MyBaseClass obj = new MyBaseClass();
obj.foo(1,”shakti”);
obj.foo(“Test”,2);


Here is the output:-

The parameter order is int and string.The value of i is 1 and that of s is shakti
The parameter order is string and int.The value of i is 2 and that of s is Test

5. Data type of parameters


public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("The is one parameter function which takes int as parameter and value of i is {0}.",i);
}
public void foo(string s )
{
Console.WriteLine("The is one parameter function which takes string as parameter and value of s is {0}.", s);
}
}


Here the number of parameters are same but data types are different so this will compile fine.

Lets now create object of this class and call both the versions:-


MyBaseClass obj = new MyBaseClass();
obj.foo(10);
obj.foo("Test");


Output:-

The is one parameter function which takes int as parameter and value of i is 10.
The is one parameter function which takes string as parameter and value of s is Test.

6. Overloading in case of inheritance


public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("The is one parameter function which takes int as parameter and value of i is {0}.",i);
}
}

public class MyChildClass : MyBaseClass
{
public void foo(string s)
{
Console.WriteLine("The is one parameter function which takes string as parameter and value of s is {0}.", s);
}
}


Here overloading is done on a function present in base class this is different from method hiding in way that hiding hides the implementation of base class while in this case both the implementations will co-exist in child class.

Lets now create object of this class and call both the versions:-


MyBaseClass obj = new MyBaseClass();
obj.foo(10);
MyChildClass obj2 = new MyChildClass();
obj2.foo("Test");


Here is the output:-

The is one parameter function which takes int as parameter and value of i is 10.
The is one parameter function which takes string as parameter and value of s is Test.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Function overloading in c#  .  Function overloading  .  Compile time polymorphism in c#  .  Compile time polymorphism  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Differences between const and readonly
Previous Resource: Compile Time Polymorphism – Operator Overloading in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use