Scientech Easy 8 Java Method Overloading Interview Programs for Practice | Scientech Easy

Sunday, March 17, 2019

8 Java Method Overloading Interview Programs for Practice

This tutorial will guide you such 8 best Java practical interview programs on method overloading with key points for practice which are always a chance to ask in each interview whether it is a technical test or technical interview. If you go without preparation of these kinds of questions, I think that your chance will be less for selection. Therefore, you solve these kinds of questions, your selection chance will be increased.



Java Interview Questions on Method Overloading

Java method overloading interview programs

Question 1:
    package methodOverloading; public class A { } public class B extends A { } public class C extends B { }
Consider the above program source code. What will be the output of the following scenarios?
Scenario 1:
    public class OverLoadingScenarios { void m1(A a){ System.out.println(" I am in m1 A"); } void m1(B b){ System.out.println("I am in m1 B"); } void m1(C c){ System.out.println("I am in m1 C"); } } public class OverLoadingTest { public static void main(String[] args) { OverLoadingScenarios obj=new OverLoadingScenarios(); //Scene 1: A a=new A(); obj.m1(a); // Scene 2: B b=new B(); obj.m1(b); // Scene 3: C c=new C(); obj.m1(c); // Scene 4: B bc=new C(); obj.m1(bc); // Scene 5: A ab=new B(); obj.m1(ab); } }

    Output: Scene 1: I am in m1 A Scene 2: I am in m1 B Scene 3: I am in m1 C Scene 4: I am in m1 B Scene 5: I am in m1 A
1. Scene 1: 'a' is the reference variable of class A  and pointing to the objects of class A. In resolving method overloading, During compilation, the compiler does not check the types of object to which particular reference variable is pointing. It just checks that which method is called through the object reference variable. It also checks method definition exists or not.
In scene 1, 'a' is the reference variable of class A. Therefore, the compiler will call the m1() method of A because it is exactly matched. Similarly, for scene 2, and 3.

2. Scene 4: 'bc' is the reference variable of class B and it is pointing to the objects of class C but the compiler will call the m1() method of class B because bc is the reference variable of class B. Similarly for scene 5. 
Key points: 
In method overloading, method resolution takes place entirely at compile time and It is always resolved by the compiler based on reference type only. In overloading, runtime object does not play any role.
Scenario 2:
    package methodOverloading; public class OverLoadingScenarios2 { void m1(A a){ System.out.println(" I am in m1 A"); } void m1(B b){ System.out.println("I am in m1 B"); } void m1(C c){ System.out.println("I am in m1 C"); } public static void main(String[] args) { OverLoadingScenarios2 obj=new OverLoadingScenarios2(); obj.m1(null); } }
    Output: I am in m1 C
Key points: 
A is the parent class B, and B is the parent class of C. So, C is the child class of B. A call to m1() method with a null argument executes the third version of m1() method with argument c of type C because while resolving overloaded method, the compiler always uses the presidency for child type argument. In this kind of case, you always see the parent-child relationship.
For practice more questions, go to this link: Automatic type promotion in method overloading Java

Note: A field of an object type can have a null value in Java. The default value for string and an object type argument is null.
Scenario 3:
    package methodOverloading; public class OverLoadingScenarios3{ void m1(A a){ System.out.println(" I am in m1 A"); } void m1(B b){ System.out.println("I am in m1 B"); } void m1(Object o){ System.out.println("I am in m1 C"); } public static void main(String[] args) { OverLoadingScenarios3 obj=new OverLoadingScenarios3(); obj.m1(null); } }
    Output: I am in m1 B
Key points: 
An object is the superclass of A and B where A is the superclass of B. Thus, B is the child class. Therefore, the output is "I am in m1 B".
Scenario 4:
    package methodOverloading; public class OverLoadingScenarios4 { void m1(A a){ System.out.println(" I am in m1 A"); } void m1(String s){ System.out.println("I am in m1 B"); } void m1(Object o){ System.out.println("I am in m1 C"); } public static void main(String[] args) { OverLoadingScenarios2 obj=new OverLoadingScenarios2(); obj.m1(null); } }
    Output: Unresolved compilation problem: The method m1(A) is ambiguous for the type OverLoadingScenarios2
Key points: 
In this scenario, the Object class is the superclass of string class and class A at the same level and there is also no parent-child relationship between string class and class A. When we will pass the null argument for calling m1() method, we have created an ambiguous situation for the compiler that cannot determine which method to call because all three methods are exact matches for our call. 
Therefore, the compiler complains of an error that the call is ambiguous and it will generate compile time error. 
Note: An overloaded method cannot be ambiguous its own. It only becomes ambiguous if you create an ambiguous situation.

Question 2. What will be the output of the following program?
    package methodOverloading; public class XYZ { void msg(Object obj){ System.out.println("Good"); } void msg(String str){ System.out.println("Better"); } void msg(Integer itr){ System.out.println("Best"); } public static void main(String[] args) { XYZ obj=new XYZ(); obj.msg(new Object()); // Exact matched to call m1() method with Object type argument. obj.msg("Scientech Easy"); // Exact matched to call m1() method with String type argument. obj.msg(new XYZ()); // Exact matched to call m1() method with Object type argument. obj.msg(new String()); obj.msg(10); // Exact matched to call m1() with Integer type argument. obj.msg(new Integer(0)); } }
    Output: Good Better Good Better Best Best
Question 3: What will be the output of the below program?
    package methodOverloading; public class XYZ { void msg(Object obj){ System.out.println("Good"); } void msg(String str){ System.out.println("Better"); } void msg(Integer itr){ System.out.println("Best"); } public static void main(String[] args) { XYZ obj=new XYZ(); obj.msg(null); // Created an ambiguous situation for the compiler to call } }
    Output: Compile time error: The method msg(Object) is ambiguous for the type XYZ
Question 4: What is the output of the below program?
    package methodOverloading; public class Overloaded { public static void m1(int a){ System.out.println("int"); } public static void m1(short a){ System.out.println("short"); } public static void m1(Object a){ System.out.println("object"); } public static void m1(String a){ System.out.println("String"); } public static void main(String[] args) { byte b=5; m1(b); // First call m1(5); // Second call Integer i=10; m1(i); // Third call m1("10"); // Fourth call m1(null); // Fifth call } }
    Output: short int object String String
Key points: 
1. When we will call an m1() method with a byte argument, there is no m1() definition that will take byte as an argument. Since the smaller data type in size is short than int data type which is larger size as a comparison to short. Therefore, the compiler will promote byte to short. The result will be short.
2. In the second method call, an exact match is found to call m1() with int argument.

3. The third method call is m1(i) where i is a type of Integer. There is no such m1() method that takes an Integer type argument. Therefore, the compiler will implicitly upcast from Integer to Object type argument and it will call to m1() method with Object type argument. 
Remember that the compiler allows implicit upcast, not downcast. Therefore, m1(int a) will not be considered.

4. In the fourth method call, an exact match is found to call m1() with a String argument. 
5. The last method call is m1(null). Since null is valid for both object and string. So, which method will be called? m1(String a) is called because the string is the child class of object class.
Question 5: What is the output of the below program source code?
    package methodOverloading; public class Overloaded { public static void msg(long a, int b){ System.out.println("Hello"); } public static void msg(int a, long b){ System.out.println("Hi"); } public static void main(String[] args) { msg(5l, 10); msg(10,11); } }
    Output: Hello Unresolved compilation problem: The method msg(long, int) is ambiguous for the type Overloaded
Question 6: What will be the outcome of the below program?
    package methodOverloading; public class Overloaded { public static void test(int[] intArr) { System.out.println("int array"); } public static void test(char[] charArr) { System.out.println("char array"); } public static void main(String[] args) { test(null); } }
    Output: Unresolved compilation problem: The method test(int[]) is ambiguous for the type Overloaded
Key points: 
int[ ] and char[ ] both are not primitive type in Java. Both are classes that extend object class at the same level. Therefore, the compiler is unable to resolve overloaded method to call.

Question 7: What will be the outcome of the below program?
    package methodOverloading; public class Overloaded { public void test(int i) { System.out.println("int"); } public void test(Number n) { System.out.println("Number"); } public void test(Integer i) { System.out.println("Integer"); } public static void main(String[] args) { Overloaded o=new Overloaded(); o.test(null); o.test(10); // Exact matched. } }
    Output: Integer int
Key points: 
Object class is the superclass of Number whereas Number is the parent class of Integer. Thus, Integer is a child class. Therefore, the result is an Integer.

Question 8: What will be the output of the following program?
    package methodOverloading; public class Overloaded { public void test(int i) { System.out.println("Int"); } public void test(int... i) { System.out.println("Int"); } public void test(char... c) { System.out.println("Char varargs"); } public static void main(String[] args) { Overloaded obj = new Overloaded(); obj.test('a'); obj.test(10); // Exact matched. } }
    Output: Int Int
Key points: 
Methods with varargs (...) have the lowest priority. That's why the output is Int.
Final words 

Hope that this article will help you to practice programs based on Java method overloading. We hope that you will have enjoyed solving these kinds of programs.Thanks for reading!