Bird
0
0

Consider this class:

hard📝 Application Q9 of 15
Java - Polymorphism

Consider this class:

class Overload {
    void process(int a) { System.out.println("int: " + a); }
    void process(Integer a) { System.out.println("Integer: " + a); }
    void process(Object a) { System.out.println("Object: " + a); }
}
public class Main {
    public static void main(String[] args) {
        Overload o = new Overload();
        o.process(5);
        o.process(Integer.valueOf(5));
        o.process("test");
    }
}

What is the output?

ACompilation error
BInteger: 5\nInteger: 5\nObject: test
Cint: 5\nInteger: 5\nObject: test
Dint: 5\nObject: 5\nObject: test
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first call o.process(5)

    Primitive int matches process(int a) exactly.
  2. Step 2: Analyze second call o.process(Integer.valueOf(5))

    Matches process(Integer a) exactly.
  3. Step 3: Analyze third call o.process("test")

    String is an Object, so process(Object a) is called.
  4. Final Answer:

    int: 5\nInteger: 5\nObject: test -> Option C
  5. Quick Check:

    Overloading chooses most specific match [OK]
Quick Trick: Primitive matches exact, wrapper matches wrapper, else Object [OK]
Common Mistakes:
  • Assuming Integer call goes to Object
  • Confusing primitive and wrapper types
  • Expecting compilation error due to overloads

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes