Method Overloading in Java with examples
Method Overloading is a feature that allows a class to have
two or more methods having same name, if their argument lists are different. In
the last tutorial, we discussed constructor overloading that allows a class to
have more than one constructors having different argument lists.
Argument lists could differ in –
1. Number of parameters.
2. Data type of parameters.
3. Sequence of Data type of parameters.
Method overloading is also known as Static Polymorphism.
Points to Note:
1. Static Polymorphism is also known as compile time binding
or early binding.
2. Static binding happens at compile time. Method
overloading is an example of static binding where binding of method call to its
definition happens at Compile time.
Method Overloading examples:
As discussed above, method overloading can be done by having
different argument list. Let’s see examples of each and every case.
Example
1: Overloading – Different Number of parameters in argument list
When methods name is same but number of arguments are
different.
In the above example – method disp() has been overloaded
based on the number of arguments – We have two definition of method disp(), one
with one argument and another with two arguments.
Example
2: Overloading – Difference in data type of arguments
In this example, method disp() is overloaded based on the
data type of arguments – Like example 1 here also, we have two definition of
method disp(), one with char argument and another with int argument.
Example3:
Overloading – Sequence of data type of arguments
Here method disp() is overloaded based on sequence of data
type of arguments – Both the methods have different sequence of data type in
argument list. First method is having argument list as (char, int) and second
is having (int, char). Since the sequence is different, the method can be
overloaded without any issues.
Let’s see
few Valid/invalid cases of method overloading
Case 1:
Result: Compile time error. Argument lists are exactly same.
Both methods are having same number, data types and same sequence of data types
in arguments.
Case 2:
Result: Perfectly fine. Valid case for overloading. Here
data types of arguments are different.
Case 3:
Result: Perfectly fine. Valid case for overloading. Here
number of arguments are different.
Case 4:
Result: Perfectly fine. Valid case for overloading. Sequence
of the data types are different, first method is having (int, float) and second
is having (float, int).
Case 5:
Result: Compile time error. Argument lists are exactly same.
Even though return type of methods are different, it is not a valid case. Since
return type of method doesn’t matter while overloading a method.
Guess the answers before checking it at the end of programs:
Question 1 – return type, method name and argument list
same.
Answer:
It will throw a compilation error: More than one method with
same name and argument list cannot be defined in a same class.
Question 2 – return type is different. Method name &
argument list same.
Answer:
It will throw a compilation error: More than one method with same name and argument list cannot be given in a class even though their return type is different. Method return type doesn’t matter in case of overloading.
It will throw a compilation error: More than one method with same name and argument list cannot be given in a class even though their return type is different. Method return type doesn’t matter in case of overloading.
0 comments:
Post a Comment