Method Overloading in JAVA

The process of writing two or more methods with the same name refers to as Method Overloading.

In order to resolve a function call for the overloaded method, the following rule should be applied.

  1. Number of parameters should be different.
  2. Type of parameter should be different.
  3. Order of parameter should be different.

Example:

class Addition{
     void add(){
     System.out.println("This is add method");
     }
     void add(int x, int y){
     int z=x+y;
     System.out.println(z);
     }
     void add(int x, float y){
     int z=x+y;
     System.out.println(z);
     }
     void add(double x, float y){
     int z=x+y;
     System.out.println(z);
     }
}

class DemoAddition{
     public static void main(String[] args){
     int a,b;
     float c,d;
     double e,f;

     Addition A = new Addition();

     A.add();
     A.add(int a, int b);
     A.add(int a, float c);
     A.add(double e, float d);
     }
}

JAVA implements the concept of polymorphism using method overloading. This polymorphism is a virtual polymorphism. The main reason for this is that one method could not perform multiple jobs rather for each of the job a separate method is present.

In method overloading, nothing is actually over loaded. It is just an illusion that one method can exhibit multiple functionalities, one method can only perform one task.

This is the main reason why method overloading is referred as virtual polymorphism.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: