nerdexam
Oracle

1Z0-808 · Question #3

class A { public A(){ System.out.print ("A "); } } class B extends A{ public B(){ //line n1 System.out.print ("B "); } } class C extends B{ public C(){ //line n2 System.out.print ("C "); } public…

The correct answer is C. A B C. Option C (A B C) is correct because Java implicitly inserts a super() call as the first statement in any constructor that doesn't explicitly call super() or this(). So when new C() is invoked, C's constructor calls B's constructor, which calls A's constructor - execution then…

Working with Inheritance

Question

class A { public A(){ System.out.print ("A "); } } class B extends A{ public B(){ //line n1 System.out.print ("B "); } } class C extends B{ public C(){ //line n2 System.out.print ("C "); } public static void main(String[] args) { C c = new C(); } } What is the result?

Options

  • AC B A
  • BC
  • CA B C
  • DCompilation fails at line n1 and line n2

How the community answered

(29 responses)
  • A
    14% (4)
  • B
    7% (2)
  • C
    76% (22)
  • D
    3% (1)

Explanation

Option C (A B C) is correct because Java implicitly inserts a super() call as the first statement in any constructor that doesn't explicitly call super() or this(). So when new C() is invoked, C's constructor calls B's constructor, which calls A's constructor - execution then unwinds back down the chain, printing "A", then "B", then "C".

Why the distractors are wrong:

  • A (C B A) reverses the order - constructors complete from top-down (ancestor first), not bottom-up.
  • B (C) would only be true if child constructors could skip calling their parent - they cannot; the implicit super() is mandatory.
  • D (Compilation fails) is wrong because the implicit super() insertion is done by the compiler automatically; neither n1 nor n2 have any issue.

Memory tip: Think of it as "you can't exist before your parents" - Java always constructs the ancestry chain from the oldest ancestor (ObjectABC) before the child can finish initializing.

Topics

#constructor chaining#implicit super()#inheritance hierarchy#constructor execution order

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice