Here, we have listed a sample of top 8 Java method overloading interview programs for practice. All these questions are very important that can be asked in technical tests or interviews for freshers and 1 to 3 years of experience.

If you try to solve all these questions based on method overloading concept, your concept will be more clear and could be able to solve questions easily in the technical test.

Java Interview Questions on Method Overloading



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 Explanation:

1. Scene 1: ‘a’ is the reference variable of class A and pointing to object of class A. In resolving method overloading, during compilation, Java compiler does not check 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 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 object of class C but Java compiler will call 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:

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 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:

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:

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