Skip to main content

What is Method Overloading in Java ??

We can have multiple methods with the same name as long as their parameter types or number of parameters differ. These methods are called overloaded methods. This is also referred as Static Polymorphism.
Points To Remember
  • Overloading of methods is always done in the same class.
  • The access specifier of the overloaded method may or may not be same  to the other overloaded methods.
  • Name of the overloaded methods should be same.
  • The overloaded methods may have different return types.
  • Overloaded methods should have at least one of the following
    • Different argument types
    • Different number of arguments
  • They provide Static or Compile time Polymorphism.
Example Of Overloading
The following is a simple example of overloading.
class Derived {

public void show() {
System.out.println("show()");
}
public void show(String args) {
System.out.println("show("+ args +")");
}
public void show(int a){
System.out.println("show("+ a +")");
}
public void show(String abc, int a) {
System.out.println("show("+ abc +", "+ a +")");
}
public static void main(String[] args) {
Derived obj = new Derived();
obj.show();
obj.show("String");
obj.show(10);
obj.show("String", 5);
}
}
The above example will give an output
show()
show(String)
show(10)
show(String, 5)

Thus it is clear that we can have more than one method with same name in the same class as long as the number of arguments they take is different or the type of arguments are different.

Overloading  Use Case
Suppose we want to make class that finds the area of any shape, this class should have methods to calculate the area of all types of shapes with the same method name area.
We can do this in the following ways.
  • We can have a speak() method inside each class Dog, Cat or Duck and we can access the speak() method by making object of that class. This method is a bad practice and must be avoided.
  • We make a super class Animal and all other classes like Dog, Cat etc extend this class. Now we can give reference of the Animal class to all the objects of Dog, Cat , Duck etc.

class Area {

public double area(double radius) {
return ((22 / 7 ) * radius * radius);
}
public int area(int side) {
return side * side ;
}
public int area(int length, int width){
return length * width ;
}

public static void main(String[] args) {

Area shape = new Area();

System.out.println("Area of circle = " + shape.area(5.0));
System.out.println("Area of square = " + shape.area(5));
System.out.println("Area of rectangle =" + shape.area(5,5));
}
}

Output of the above example is as follows
I am a dog and i do Woof Woof
I am a cat and i do Meow Meow
I am a duck and i do Quack Quack
My language is not known


In the above example the method speak() of class Test takes the parameter animal object and prints what does that animal speaks. This method does not know about what type of animal it is having. Its only gets to know at runtime.

This is how Polymorphism(Dynamic Binding) works and this example shows how you can achieve polymorphism with the help of Method Overriding.


Comments