Open In App

Output of Java program | Set 22 (Overloading)

Last Updated : 03 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Overloading in Java
1) 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 error
Ans. (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 error
Ans. (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 these
Ans. (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 error
Ans. (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) GeeksforGeeks
Ans. (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) GeeksforGeeks
Ans. (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.

 



Similar Reads

Method Overloading in Java
In Java, Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both. Method overloading in Java is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding. In Method overloading c
9 min read
Overloading Variable Arity Method in Java
Here we will be discussing the varargs / variable arity method and how we can overload this type of method. So let us first understand what a variable arity method is and its syntax. A variable arity method also called as varargs method, can take a number of variables of the specified type. Note: Until version 1.4 there is no varargs method. It was
5 min read
Invalid Method Overloading in Java
Overloaded methods are those which belong to the same class, having the same name but different arguments. The concept of method overloading came from polymorphism. Literally "poly" means many and "morphism' means form. Consider a real-life example of water as polymorphic because it can take many forms (Solid, Liquid, and gas). Similarly, in java,
4 min read
Automatic Type Promotion in Overloading in Java
Before going into the actual topic, first, we need to know about method overloading and type promotions. What is Method Overloading?When a class consists of more than one method with the same name but with different signatures and return types, then we call those overloaded methods, and the process is called method overloading. Example: void method
6 min read
GFact 48 | Overloading main() in Java
Consider the below Java program. // A Java program with overloaded main() import java.io.*; public class Test { // Normal main() public static void main(String[] args) { System.out.println("Hi Geek (from main)"); Test.main("Geek"); } // Overloaded main methods public static void main(String arg1) { System.out.println("Hi,
2 min read
Different ways of Method Overloading in Java
If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.If we have to perform only one operation, having same name of the methods increases the readability of the program.Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you
7 min read
Method Overloading and Ambiguity in Varargs in Java
Prerequisite - Varargs , Method Overloading Method Overloading in Varargs Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. We can overload a method that takes a variable-length argument by following ways: Case 1 - Methods wi
5 min read
Java Function/Constructor Overloading Puzzle
Predict the output of the program public class GFG { private GFG(Object o) { System.out.println("Object"); } private GFG(double[] d) { System.out.println("double array"); } public static void main(String[] args) { new GFG(null); } } Solution: The parameter passed to the constructor is the null object reference and arrays are ref
1 min read
Difference Between Method Overloading and Method Overriding in Java
The differences between Method Overloading and Method Overriding in Java are as follows: Method Overloading Method Overriding Method overloading is a compile-time polymorphism.Method overriding is a run-time polymorphism.Method overloading helps to increase the readability of the program.Method overriding is used to grant the specific implementatio
4 min read
Output of Java Program | Set 1
Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java Code // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); } } Output: 0 0 In Java, a protected member is accessible in all cl
3 min read