Overloading or Polymorphism

Polymorphism means many forms. In object oriented programming the concept of polymorphism or overloading is very important to learn and it helps a lot in simplifying the code that you write. Overloading gives you the power to define many functions with the same name but different parameters inside an object. The simple definition of overloading is

When an object has two or more functions with the same name but different arguments we define this as overloading or polymorphism.

We humans demonstrate overloading on a daily basis. Let us take an example to explain overloading in practical terms.

You have to go to a party tonight and you do not want to go alone. So you decide to ask your friend to come along with you. You ask your friend and she agrees to go to the party with you. The party starts at 8 pm in the evening. So you tell this to your friend

  1. Be ready
  2. I will pick you up
  3. And we will go to the party

When you go to pick her up in the evening you see that she is dressed in her party wear. From there you go to the party together.
Next day you both plan to go for a badminton match to the club at 4pm in the evening. This is what you tell your friend

  1. Be ready
  2. I will pick you up
  3. And we will go for the match

When you go to pick her up you see that she is dressed in her track suits and is wearing sports shoes. You both go to the club and play badminton.

For both the events you find that your friend dresses up differently for both the occasions. Did you tell her to dress in a party wear for the party and the track suit for the badminton match? No. For both the events you told her the exact same things

  1. Be ready
  2. I will pick you up
  3. And we will go for the party / match

Notice that the only difference between both the statements is the event for which you have to go. According to the event which you mention you see that your friend dresses up differently. This is what we call as overloading. Humans are intelligent enough to change their behaviour for the same thing according to the data / information we receive. Similarly when you write code you want your objects to behave intelligently by looking at the parameters we pass and deciding what to do. Overloading gives you the power to create two or more functions with the same name but different arguments / parameters. The object decides which function to call according to the arguments which you pass to the function.

Let’s take a code example


Class Test
{

Public void Function1(int X)
{
}

Public void Function1(string s)
{
}


Notice that in the Test class there are two functions with the same name Function1 . Both these functions have different arguments. The first function is taking an integer type argument whereas the second function is taking a string type argument. When you will create an object of this class it will have two functions with the same name and it will know which one to call depending on the type of argument you pass while calling the function.

When and where you can use overloading?

You will need overloading in almost every software project which you will write using an object oriented language. Even procedural programming languages like C have overloading in them. Look at the two print statements written using the C language

printf(“Hello world”); // The printf function here takes only one argument.
Result : Hello world

printf(“Hello %s”, “Ravinder”); // The printf function here takes two arguments.
Result : Hello Ravinder

We have called the same function in the C language with different arguments. The first printf function takes one string type argument and will print it as it is. The second printf function takes two string type arguments - first argument is a string with a placeholder and the second argument is the data to put at the placeholder. When printf function will notice two arguments being passed to it it will change its behaviour. It now knows that it has to first process the first string argument by replacing the place holder %s in it with the second argument and will print the result as “Hello Ravinder”.

The overloading principle gives you the power to make your objects intelligent by executing different functions of the same name depending on the arguments passed to them during the function calls.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp