Method overloading in java

Method Overloading in java

Method overloading is a feature in Java that allows a class to have more than one method which has the same name, even if their arguments vary. It is also done within the same class with different parameters. Overloading is sometimes also referred to as compile-time polymorphism. Overriding is a similar concept in java. It varies with method overloading as it is performed in different classes having the same parameters. One class is the parent class and the other is the child class. Overriding methods have the inheritance relationship within the classes.

Read about Polymorphism in JAVA

There are different ways of performing overloading:

  • Overloading by changing the number of arguments.
  • By changing the data types of the parameters.
  • By changing the order of the parameters

The main advantages of overloading are:

  • More than two methods can have the same name inside that same class with different arguments.
  • We cannot perform method overloading by changing the return type of the arguments.
  • It maintains the cleanliness of the code.
  • It increases the quality of reading of a program, along with its flexibility.
  • The method can also be used as a constructor for creating new objects.
  • It can take a huge amount of data.
  • Ambiguities can be avoided by using different numbers and types of arguments.

Method overloading Examples

public class Subtraction { 
    public int subtraction (int a, int b){ 
        return (a - b); 
    } 
    public int subtraction (int a, int b, int c){ 
        return (a - b - c); 
    } 
    public double subtraction (double a, double b){ 
        return (a - b ); 
    } 
    public static void main(String args[]) { 
        Subtract s = new Subtract(); 
        System.out.println(s.subtraction(300, 200)); 
        System.out.println(s.subtraction(300, 200, 100)); 
        System.out.println(s.subtraction(300.5, 200.5)); 
    } 
} 

Output :

100
0
100

From the above example, we can see that we don’t have to remember different names for functions that are performing the same function in the program. For example, in the code, if overloading was not performed by Java, we would have to create method names like subtraction1, subtraction2, … or subtraction2Int, subtraction3Int, … etc.

However, we cannot perform method overloading by changing the return type of the method. It is because of the rise of ambiguity in the code. It can generate a compile-time error while compilation. Below is a program for elaboration:

class Subtractor{  
static int subtract(int x, int y)
{
return x-y;
}  
static double subtract(int x, int y)
{
return x-y;
}  
}  
class Overloading
{  
public static void main(String[] args)
{  
System.out.println(Subtractor.subtract(200,100));  
}
}

The output will be:

Compile Time Error: method add(int,int) is already defined in class Adder

The frequent question that arises while executing the process of method overloading is can we overload the main function in java?

The answer is yes. We can overload the main() in java by method overloading. We can have an unlimited number of main() in a single class by method overloading. The main() can be called inside public static void main(String args[]) in JVM.

It is also possible to overload static methods in java by having multiple static methods with the same name and different input parameters. However, we cannot overload two methods if they have the different static keyword in the code.

Difference between method Overloading and Method Overriding

As we have discussed in the previous section, method overloading is always performed in the same classes. However, the process of method overriding is performed between the parent and the child class. Overriding is the process where the child class inherits the methods of the parent class using the IS-A relationship.

Method overloading is an example of compile-time polymorphism whereas method overriding is an example of run time polymorphism. There are no hard rules for return type in method overloading. The return type can either be the same or different in this case. However, in the case of method overriding, the return type has to be the same.

It is also possible to overload static methods in java by having multiple static methods with the same name and different input parameters. However, we cannot overload two methods if they have the different static keyword in the code.

One comment on “Method Overloading in java

  • KsiΔ…ΕΌki Psychologia , Direct link to comment

    Thanks for the nice writing. I have bookmarked your site and will be back to read more!

Leave a comment

Your email address will not be published. Required fields are marked *