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. Lets see examples of each and every case.
Example 1: Overloading – Different Number of parameters in argument list
When methods name are same but number of arguments are different.
class DisplayOverloading { public void disp(char c) { System.out.println(c); } public void disp(char c, int num) { System.out.println(c + " "+num); } } class Sample { public static void main(String args[]) { DisplayOverloading obj = new DisplayOverloading(); obj.disp('a'); obj.disp('a',10); } }
Output:
a a 10
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.
class DisplayOverloading2 { public void disp(char c) { System.out.println(c); } public void disp(int c) { System.out.println(c ); } } class Sample2 { public static void main(String args[]) { DisplayOverloading2 obj = new DisplayOverloading2(); obj.disp('a'); obj.disp(5); } }
Output:
a 5
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.
class DisplayOverloading3 { public void disp(char c, int num) { System.out.println("I’m the first definition of method disp"); } public void disp(int num, char c) { System.out.println("I’m the second definition of method disp" ); } } class Sample3 { public static void main(String args[]) { DisplayOverloading3 obj = new DisplayOverloading3(); obj.disp('x', 51 ); obj.disp(52, 'y'); } }
Output:
I’m the first definition of method disp I’m the second definition of method disp
Lets see few Valid/invalid cases of method overloading
Case 1:
int mymethod(int a, int b, float c) int mymethod(int var1, int var2, float var3)
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:
int mymethod(int a, int b) int mymethod(float var1, float var2)
Result: Perfectly fine. Valid case for overloading. Here data types of arguments are different.
Case 3:
int mymethod(int a, int b) int mymethod(int num)
Result: Perfectly fine. Valid case for overloading. Here number of arguments are different.
Case 4:
float mymethod(int a, float b) float mymethod(float var1, int var2)
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:
int mymethod(int a, int b) float mymethod(int var1, int var2)
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.
class Demo { public int myMethod(int num1, int num2) { System.out.println("First myMethod of class Demo"); return num1+num2; } public int myMethod(int var1, int var2) { System.out.println("Second myMethod of class Demo"); return var1-var2; } } class Sample4 { public static void main(String args[]) { Demo obj1= new Demo(); obj1.myMethod(10,10); obj1.myMethod(20,12); } }
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.
class Demo2 { public double myMethod(int num1, int num2) { System.out.println("First myMethod of class Demo"); return num1+num2; } public int myMethod(int var1, int var2) { System.out.println("Second myMethod of class Demo"); return var1-var2; } } class Sample5 { public static void main(String args[]) { Demo2 obj2= new Demo2(); obj2.myMethod(10,10); obj2.myMethod(20,12); } }
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.
In Java, what’s the difference between method overloading and method overriding?
The difference between overriding and overloading in Java is a common source of confusion – but it is fairly easy to understand with the examples we present below. Let’s start the discussion by talking more about method overloading first. Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters (remember that method parameters accept values passed into the method). Now, two or more methods with the same name in the same class sounds simple enough to understand. But, what do we mean exactly by differentparameters? Well, let’s consider a very simple example.
Suppose we have a class called TestClass which has two methods, and both methods have the same name. Let’s say that name is “someMethod”. Those two methods would be considered to be “overloaded” if if one or both of these conditions is true:
The conditions for method overloading
1.) The number of parameters is different for the methods. 2.) The parameter types are different (like changing a parameter that was a float to an int).
How to NOT overload methods:
It’s also very important to understand that method overloading is NOT something that can be accomplished with either, or both, of these two things:
1. Just changing the return type of the method. If the return type of the method is the only thing changed, then this will result in a compiler error. 2. Changing just the name of the method parameters, but not changing the parameter types. If the name of the method parameter is the only thing changed then this will also result in a compiler error.
Confused? Well, here are some very helpful examples of where overloading would be both valid and invalid – pay attention to the comments as well:
Examples of Method Overloading in Java – both valid and invalid:
//compiler error - can't overload based on the //type returned - //(one method returns int, the other returns a float): int changeDate(int Year) ; float changeDate (int Year); //compiler error - can't overload by changing just //the name of the parameter (from Year to Month): int changeDate(int Year); int changeDate(int Month) ; //valid case of overloading, since the methods //have different number of parameters: int changeDate(int Year, int Month) ; int changeDate(int Year); //also a valid case of overloading, since the //parameters are of different types: int changeDate(float Year) ; int changeDate(int Year);
Overloading happens at compile time
Another important point to remember is that overloading is a compile time phenomenon. This just means that the compiler determines whether a given method(s) is correctly overloaded, and if not a compiler error is returned as shown in the examples above.
What about method overriding?
Overriding methods is completely different from overloading methods. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.
Example of method overriding
Let’s go through a simple example to illustrate what method overriding would look like:
public class Parent { public int someMethod() { return 3; } } public class Child extends Parent{ // this is method overriding: public int someMethod() { return 4; } }
In the sample code above, someMethod is an overridden method in the Child class, because it has the exact same name, number of parameters, and return type as the someMethod method defined inside it’s parent class (conveniently named Parent).
Overriding happens at run time
Another important point to remember is that overriding is a run time phenomenon – not a compile time phenomenon like method overloading.
Summary of differences between overloading and overriding
Let’s summarize the differences between overloading and overriding. When overloading, one must change either the type or the number of parameters for a method that belongs to the same class. Overriding means that a method inherited from a parent class will be changed. But, when overriding a method everything remains exactly the same except the method definition – basically what the method does is changed slightly to fit in with the needs of the child class. But, the method name, the number and types of parameters, and the return type will all remain the same.
And, method overriding is a run-time phenomenon that is the driving force behind polymorphism. However, method overloading is a compile-time phenomenon.
What is method
overloading? [duplicate]
vorite |
I've found resources that say method overloading is the
ability for a language to use the same method with a different outcome,
depending on context. Somehow, when I read other definitions, I fell like
that's not the whole definition. Is there more to method overloading?
|
||