Method Overloading
Overloaded methods let you reuse the same method name in a class, but with different arguments.
For example, you can have a method that takes integer parameters and another method with same name but taking string or some other parameters.
Here are two add() methods depicting this :
void add(int number1, int number 2)
void add(String str1, String str2)
Overloading methods can be present in the same class or in the inherited class.
Overloading guidelines:
Here are the rules for overloading :
– A method can be overloaded in same class or in the child class too.
– An overloaded method must change the argument list.
– An overloaded method may optionally change the return type or access modifier.
– An overloaded method can throw newer/broader exceptions.
Overloading methods in same class
Here is an example class that provides two overloaded print methods.
One method takes a String argument and the other has no arguments. The methods are invoked depending on whether the user provides a parameter while calling the method or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.tutorial; public class HelloPrint1{ public void print(){ System.out.println("Hello"); } public void print(String name){ System.out.println("Hello " + name); } public static void main(String[] args){ HelloPrint1 printer = new HelloPrint1(); printer.print(); printer.print("user"); } } |
Running this class produces following output :
Hello
Hello user
Overloading a method in Derived class and Overriding
Methods can be overloaded in derived classes as well.
It gets little more trickier to find whether a method is overloaded or overridden in the derived class. You have to check the method arguments closely for this. The method arguments will vary between overloaded methods.
Here is an example where we are both overloading and overriding a method in child class :
Parent class:
1 2 3 4 5 6 7 8 9 10 11 | package com.tutorial; public class HelloPrint { public void print(){ System.out.println("Parent class method called"); } } |
Child class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.tutorial; public class HelloPrint1 extends HelloPrint{ public void print(){ System.out.println("Overriding method in child class called"); } public void print(String name){ System.out.println("Overloaded method in child class called"); } public static void main(String[] args){ HelloPrint1 printer = new HelloPrint1(); printer.print(); printer.print("user"); } } |
Running the child class will produce following output :
Overriding method in child class called
Overloaded method in child class called
In the child class, the print() method overrides the same method defined in parent class.
The method print(String) overloads the print() method.
Method Overloading with varargs
Methods with varargs can accept zero or more parameters. So, if you have a method taking a single parameter and a Vararg method with same name and you call the method, java chooses the fixed argument method over the vararg.
Here is an example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void main(String[] args) { method(1); } public static void method(int x){ System.out.println("Single argument method"); } public static void method(int... v){ System.out.println("Variable argument method"); } |
Output of this program will be :
Single argument method
So, what will happen when two methods with variable arguments are overloaded ?
Here is a quiz for the same :
Method overloading with varargs – Java Quiz 17
© 2015 – 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post