jump to navigation

What is Method Overloading in OOP? March 8, 2007

Posted by ordinarywebguy in Java, OOP.
trackback

Different OOP programming languages were strong typed language such as Java and C++, it means that methods and variables type should be defined (int, float, double, string, etc.) that’s the reason why there is method overloading. Method overloading is having methods in a class that have same method name but different in parameters. It is a form  of Polymorphism.
For example in this code:

class Compute{

public void Add(int param1, int param2) {
//do addition computation here…
}
public void Add(float param1, int param2) {
//do addition computation here…
}
public void Add(int param1, float param2) {
//do addition computation here…
}
}// end class

Now, if we talk about numbers its data type could be a int, double or float. Right? In the code we could then pass those types in method “Add()” to be able to execute addition.

class MyClass{

public static void main (String[] args) {

Compute comp = new Compute;
comp.Add(1,1);
comp.Add(5.5,1);
comp.Add(10000000000000,999999999999999);

}

} // end class

That’s it the “Method Overloading”.

Comments»

1. himanshu - June 1, 2008

please tell me difference b/w overloading of a constructor and overloading of a simple fn.
at himrox214@yahoo.co.in

2. Abdul Rahman - August 9, 2009

That was simple and understable example. Good! I liked it.