The Best Technical Blog Ever

twitterrss

Method Overloading in Java

In many circumstances, you may want to use the same method name for multiple methods since they have similar functionality. But they differs from each other by its type or number of parameters. It is known as Method Overloading. Such methods are called Overloaded Methods.


In Method Overloading, no two methods within the same class can have the same signature. In General, Method overloading occurs when two or more methods in the same class have the same method name but different parameters (type or number of parameters passed in). Only methods declared in the same class and those that are inherited by the class can be overloaded. Static methods can also be overloaded.


case 1: Method Overloading by changing the number of parameters in argument list
void test() //no parameter
public int test(int a) //one parameter
public int test(int a, int b) //two parameter

case 2: Method Overloading by changing the data type
void add(int a, double b)
void add(int a, int b)

Method overloading is also known as compile time polymorphism (static polymorphism) where you can have a several methods with the same name and the compiler will choose at compile time which one to be used depending on the parameters (type or number of parameters passed in).


Example:
method overload


One important point in Method overloading is changing just the return type  is not enough to overload a method; the parameter list of the method declarations must be different.


public int add(int a, int b)
public double add(int a, int b) //compile time error

The above statements create compile time error. Because, the return value of the method is not taken into consideration. Both method declarations have the same signature add(int, int). Thus, these two methods must not exist in the same class. It would confuse the compiler as it would not know which method is being called.


Example:
method overload


This program creates compile time error. Because both methods have the same signature.


Method Overloading and Automatic Type Conversion:

Java's automatic type conversions can affect the result of overloaded methods. In Method Overloading , the automatic conversions apply only if there is no direct match between a parameter and an argument.


Example1:
method overload


In this example, when you pass float value, Java automatically converts them to double. Thus, print(int,double) is called. However, the automatic conversions apply only if there is no direct match between a parameter and an argument.


Example2:
method overload


In Example1, there is no direct match between a parameter and an argument. So automatic type conversion occurs. When we add a method print(int,float), the automatic conversion to double does not occur and it directly calls the print method with float value. Here, compiler chooses the correct method for a particular circumstance. So no need for type conversion.


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status