An Overloaded Method gives you an option to use the same method name in a class but with a different argument.
Overloading in Java Method Rules
There are some rules associated with an overloaded method.
Overloaded methods
- Must change the argument list
- Can change the return type
- Can change the access modifier(Broader)
- Can declare a new or broader checked exception
A method can be overloaded in Class or in SubClass.
Overloading Method Example
//Overloaded method with one argument
public void add(int input1, int input2) {
System.out.println("In method with two argument");
}
//Overloaded method with one argument
public void add(int input1) {
System.out.println("In method with one argument");
}
Invoking Overloaded Method
Out of several available overloaded methods, the method invoked is based on the arguments.
add(3,4);
add(5);
The first call will execute the first method and the second will execute the second Method.
The reference type decides which overloaded Method to invoke and not the Object as opposed to Overriding the Method.
Thanks to Manoj for pointing typo error.
Method Overloading Cheatsheet
- Using the same method name but with different arguments is called overloading.
- Constructors can also be overloaded
- Overloaded Methods must have different argument set.
- Overloaded Methods may have a different return type.
- Overloaded Methods may have different access modifiers.
- Overloaded Methods may throw different exception broader or narrow no restriction
- Methods from the superclass can also be overloaded in a subclass.
- Polymorphism applies to override not overloading
- Determining which overloaded Method will be invoked is decided at compile time on the basis of the reference type.
Method Overloading Example
package com.overloading;
/*
* Here we will learn how to overload a method in the same class.
* Note: Which overloaded method is to invoke is depends on the argument passed to method.
*/
public class MethodOverloading {
public static void main(String args[])
{
//Creating object of the class MethodOverloading
MethodOverloading cls = new MethodOverloading();
System.out.println("calling overloaded version with String parameter");
cls.method("Hello");
System.out.println("calling overloaded version with Int parameter");
cls.method(3);
/*
* Here same method name has been used, But with different argument.
* Which method is to invoke is decided at the compile time only
*/
}
/*
* Overloaded version of the method taking string parameter
* name of the method are same only argument are different.
*/
void method(String str)
{
System.out.println("Value of the String is :"+str);
}
/*
* Overloaded version taking Integer parameter
*/
void method(int i)
{
System.out.println("Value of the Int is :"+i);
}
}
Hi Vivek,
In bullet points, you have mentioned “Polymorphism applies to overriding not Overloading”. Polymorphism can be applied to Method Overloading too.. which is known as compile time polymorphism or Static polymorphism
Method overloading is perfect example for compile time polymorphism and method overriding is example for run time polymorphism
this article is very clean and simple.
so thanks
REFERENCE TYPE DECIDED WHICH OVERLOADED METHOD TO INVOKE AND NOT THE OBJECT AS OPPOSED TO OVERRIDING.
What does this mean?
Can you please elaborate.
In order to understand this line you need to understand reference and object.
Test obj = new Test();
In above line obj is a reference which is actually pointing to a Object of Test class.
Now if u know reference can hold child object too.
Class TestChild extends Test{}
Test obj1 = new TestChild();
Above line is true too.
But in this case reference which is obj1 is of type Test while actual object will be of type TestChild. Which will be known only at runtime and not at compile time.
So now read that line again. Which overloaded method will be called will be determined by reference type(Obj /obj1) and not the object type(Test / TestChild).
This is opoosed to Overrding. In which case actual object will determin which method to invoke and as u know actual object type can be determined only at runtime hence it is called runtime binding while Overloding called compile time binding.
Read article after read it will clear your doubt.
Hope it help.
nice explanation!!
thanx
Not visible code within bracket what can i do
nice explain of overloading method
I is very nice but i want all notes related subject
can u help me….
Overloaded methods
Wrong : Must not change the argument list
Correct : Must change the argument list
Hi Manoj,
Thanks for pointing the error on Overloading article. Same has been resolved. But i could not understand your other point “If method can be overriden then its access modifier is changable as well.” Please elaborate so that i can change the article if required.
Also if you want to share your knowledge with others by writting article on Java related topic do let me know.
Thanks
“Can declare new or broader checked exception” can you examplify this point…