Question discussion

What is the output of the following Java program?class A { A() { show(); } void show() { System.out.print("A"); } } class B extends A { int…

1507Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

class A {
    A() { show(); }
    void show() { System.out.print("A"); }
}
class B extends A {
    int x = 5;
    void show() { System.out.print("B" + x); }
}
public class Main {
    public static void main(String[] args) { new B(); }
}
  • AB0
  • BB5
  • CA
  • DAB0
Correct answer: A

Explanation

The superclass constructor calls the overridden show() before B's field initialiser has run, so x still has its default value 0 and B0 is printed.

Discussion 0

Nobody has replied yet. Start the discussion!