Question discussion

What is the output of the following Java program?public class Main { static class A { void f(Object o) { System.out.print("O"); } void f(St…

1144Medium DSSSB TGT Computer Science 0 replies

What is the output of the following Java program?

public class Main {
    static class A {
        void f(Object o) { System.out.print("O"); }
        void f(String s) { System.out.print("S"); }
    }
    public static void main(String[] args) {
        A a = new A();
        Object o = "x";
        a.f(o); a.f("x"); a.f(null);
    }
}
  • AOOO
  • BOSO
  • CSSS
  • DOSS
Correct answer: D

Explanation

Overloads are chosen at compile time: a.f(o) uses the declared type Object (O); "x" and null both pick the most specific overload f(String) (S).

Discussion 0

Nobody has replied yet. Start the discussion!