Writing Constructors for Subclasses

AP Computer Science A· difficulty 4/5

public class A {
    public A(int x) { }
}
public class B extends A {
    public B() { }
}

What is wrong?

  • A

    Compiles and works

  • B

    Compile-time error: implicit super() does not exist in A

    check_circle
  • C

    Works only if B has a field

  • D

    Runtime error

Explanation

Since A has no no-arg constructor, the compiler-inserted <code>super()</code> in B fails. B must explicitly call <code>super(someInt)</code>.

Want 10 more like this — adaptive to your weak spots?

Related questions