Monday, November 9, 2009

METHOD OVERLOADING POLY MORPHISM (COMPILE TIME)

What is method over loading?

We can have more than one method in a class with a same name but different in number or type or the argument (parameters), this is called method overloading.

Let us see a sample program to explain this concept:

using System;

using System.Collections.Generic;

using System.Text;

namespace forblog

{

class demo

{

void add(int a, int b)

{

int c=a+b;

Console.WriteLine("The Sum is "+c);

}

void add(float x, float y)

{

float z=x+y;

Console.WriteLine("The Sum is "+z);

}

public static void Main(String []arg)

{

demo ob=new demo();

ob.add(10,20);

ob.add(23.4f,34.4f);

}

}

}

This is the sample program which contain same method name add but when you see the type of argument inside it, is different. We can also do it by making difference in the number of arguments. please try it your self.

And now …..Why it is called compile time polymorphism.

Do you know what is happening during compilation in program? During compilation the syntax of our program is checked. So, here in the above concept we are making difference in the syntax i.e. in first add method we have given two integers and in second add method, we have two float.

so this is an example for method overloading.

No comments:

Post a Comment