Java Method Overloading Interview Questions
After interviewing large set of candidates, we have come to conclusion that most of them fail to satisfactory explain basic OOPs concepts like difference between abstraction and encapsulation, static binding vs dynamic binding, co-varient return types, access modifiers scoping and checked exception handling in method overriding and overloading.
Even if they are able to explain it well, interviewer can easily confuse them by asking basic programming questions related to these topics. In this post, we will discuss all such tricky programming questions related to method overloading.
Before starting with programming questions, let's understand what is method overloading in brief.
Method Overloading
When class has multiple methods with same name but different parameters, it is known as method overloading.
Here different parameters means
- Either number of parameters can be different
-
Or type of parameters can be different
For Example,
In below program we have three overloading methods named as testMethod()
public class PumpkinDemo { public static void main(String[] args) { //it will call first method as we have passed int parameter testMethod(1); // it will call second method as we have passed String parameter testMethod("pumpkin"); // it will call third method // as we have passed two parameters and // there are no other methods with two parameters testMethod(1,"pumpkin"); } //first method public static void testMethod(int number) { System.out.println("number"); } //second method public static void testMethod(String str) { System.out.println("String"); } //third method public static void testMethod(int number, String str) { System.out.println("number, String"); } }
Output
number String number, String
Why it is known as Static Binding?
In case of method overloading, JVM can decide which method to be called at compile time based on the number and type of parameters, right?
In other words, method calls are resolved at compile time in case of method overloading. That is why it is also known as Static Binding or Static Polymorphism or Compile time Polymorphism.
Tricky Method overloading Programming Questions
Let's move further with tricky programming questions. We have listed down all such questions and their answers with explanation. But we suggest you guys to first try solving and guessing the answer of each question and then read its answer.
Question 1 What would be the output of below code?
public class PumpkinDemo { public static void main(String[] args) { testMethod(1); } public static void testMethod(long number) { System.out.println("long"); } public static void testMethod(int number) { System.out.println("int"); } public static void testMethod(Integer number) { System.out.println("Integer"); } public static void testMethod(double number) { System.out.println("double"); } public static void testMethod(Number number) { System.out.println("Number"); } }
Answer
It will print output as 'int'. Here is the preference hierarchy
int -> long -> double -> Integer Wrapper class-> Number class
In other words,
- if we will remove method with int parameter, then output will be long,
- if we will remove methods with int and long parameter, then output will be double, and so on.
Question 2) In above program, if I want to pass value as 1, but I want to call each method one by one. Write main() method code to get below output:
int long double Integer
Answer
public static void main(String[] args) { testMethod(1); testMethod(1L); testMethod(1.0); testMethod(new Integer(1)); }
Question 3) What would be the output of below code?
public class PumpkinDemo { public static void main(String[] args) { testMethod(1); } public static void testMethod(byte number) { System.out.println("byte"); } public static void testMethod(short number) { System.out.println("short"); } }
Answer
It will give compile time error as jvm can't downcast from int to byte by itself. If you will manually downcast while calling the method like testMethod((byte)1)
, then only it will get compiled and will give you output as byte.
Question 4) What would be the output of below code?
public class PumpkinDemo { public static void main(String[] args) { testMethod(null); } public static void testMethod(Long number) { System.out.println("Long"); } public static void testMethod(Integer number) { System.out.println("Integer"); } }
Answer
It will give compile time error. null creates ambiguity here as Long and Integer both can be null and JVM can not decide the method to be called.
Question 5) In above example, I want to pass null as a argument, but still i want method with String type to be called i.e. output should be String. What changes do I need to make in my main method?
Answer
public static void main(String[] args) { String s = null; testMethod(s); }
Question 6) Output of below code?
public static void main(String[] args) { testMethod(null); } public static void testMethod(Number number) { System.out.println("Number"); } public static void testMethod(Integer number) { System.out.println("Integer"); } }
Answer
Number is a parent class and Integer is its child class, so it will not create ambiguity. Priority is given to the the lowest child class i.e. Integer in our case. So answer is 'Integer'.
You can refer the Wrapper class hierarchy in below image.
Question 7) What happens if we create method with same arguments but with different return type as shown in below code. What would be the output?
public class PumpkinDemo { public static void main(String[] args) { testMethod(1); } private static Integer testMethod(Integer number) { System.out.println("Integer return Type"); } public static Number testMethod(Integer number) { System.out.println("Number Return Type"); } }
Answer:
Method Overloading has nothing to do with return type. Number of arguments or type of argument must be different else compiler will consider both of this methods as same and gives you compile time error.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: Core Java, Java, Java keywords, OOPs, Overloading, Polymorphism
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.