Question discussion

What is the output for the below code ?class A{ public A(){ System.out.println("A"); } public A(int i){ this(); System.out.println(i); }}cl…

302409Medium 1 reply 774 views
0
What is the output for the below code ?class A{ public A(){ System.out.println("A"); } public A(int i){ this(); System.out.println(i); }}class B extends A{ public B(){ System.out.println("B"); } public B(int i){ this(); System.out.println(i+3); }}public class Test{ public static void main (String[] args){ new B(5); }}
  • AA B 8
  • BA 5 B 8
  • CA B 5
  • DB 8 A 5
  • ENone of these
Correct answer: A

Discussion 1

0
CCommunity
A B 8

Constructor of class B call their superclass constructor of class A (public A()), which execute first, and that constructors can be overloaded. Then come to constructor of class B (public B (int i)).