Output of Java program | Set 22 (Overloading) - GeeksforGeeks

03 Feb, 2021
Prerequisite – Overloading in Java1) What is the output of the following program?  
Java
public class Test{    public int getData() //getdata() 1    {        return 0;    }    public long getData() //getdata 2    {        return 1;    }    public static void main(String[] args)    {        Test obj = new Test();        System.out.println(obj.getData());       }}
a) 1 b) 0 c) Runtime error d) Compilation errorAns. (d) Explanation: For method overloading, methods must have different signatures. Return type of methods does not contribute towards different method signature, so the code above give compilation error. Both getdata 1 and getdata 2 only differ in return types and NOT signatures.   2) What is the output of the following program?  
Java
public class Test{    public int getData(String temp) throws IOException    {        return 0;    }    public int getData(String temp) throws Exception    {        return 1;    }    public static void main(String[] args)    {        Test obj = new Test();        System.out.println(obj.getData("GFG"));       }}
a) 0 b) 1 c) Compilation error d) Runtime errorAns. (c) Explanation: Methods that throws different exceptions are not overloaded as their signature are still the same.3) What is the output of the following program?  
Java
public class Test{    private String function()    {        return ("GFG");    }    public final static String function(int data)    {        return ("GeeksforGeeks");    }    public static void main(String[] args)    {        Test obj = new Test();        System.out.println(obj.function());       }}
a) Compilation error b) Runtime error c) GFG d) None of theseAns. (c) Explanation: Access modifiers associated with methods doesn’t determine the criteria for overloading. The overloaded methods could also be declared as final or static without affecting the overloading criteria.4) What is the output of the following program?  
Java
public class Test{    private String function(String temp, int data)    {        return ("GFG");    }    private String function(int data, String temp)    {        return ("GeeksforGeeks");    }    public static void main(String[] args)    {        Test obj = new Test();        System.out.println(obj.function(4, "GFG"));       }}
a) GFG b) GeeksforGeeks c) Compilation error d) Runtime errorAns. (b) Explanation: The order of argument are an important parameter for determining method overloading. As the order of attributes are different, the methods are overloaded.5) What is the output of the following program?  
Java
public class Test{    private String function(String temp, int data, int sum)    {        return ("GFG");    }    private String function(String temp, int data)    {        return ("GeeksforGeeks");    }    public static void main(String[] args)    {        Test obj = new Test();        System.out.println(obj.function("GFG", 0, 20));       }}
a) GFG b) Compilation error c) Runtime error d) GeeksforGeeksAns. (a) Explanation: The order of argument are an important parameter for determining method overloading. 6) What is the output of the following program?  
Java
public class Test{    private String function(float i, int f)    {        return ("gfg");    }    private String function(double i, double f)    {        return ("GFG");    }    public static void main(String[] args)    {        Test obj = new Test();        System.out.println(obj.function(1., 20));        }}
a) GFG b) Compilation error c) Runtime error d) GeeksforGeeksAns. (a) Explanation: This one is really simple. Different types of arguments contribute towards method overloading as the signature of methods is changed with different type of attributes. Whichever method matches the set of arguments passed in the main function will be called. Here, the first argument passed is double, and hence GFG is printed.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
29AjayKumar
rutvikumak
710priyanshu
Java-Output
Java-Overloading
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to copy a string in C/C++
Runtime Errors
unsigned specifier (%u) in C with Examples
Output of C++ Program | Set 1
Output of C Programs | Set 3
How to show full column content in a PySpark Dataframe ?
Output of Java program | Set 26
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Discrete Fourier Transform and its Inverse using MATLAB
Output of python program | Set 2