Creating Superclasses and Subclasses

AP Computer Science A· difficulty 3/5

public class A {
    protected int x = 10;
}
public class B extends A {
    public int read() { return x + 1; }
}
System.out.println(new B().read());

What is printed?

  • A

    Compile-time error

  • B

    11

    check_circle
  • C

    1

  • D

    10

Explanation

<code>protected</code> allows subclasses to access the field. read() returns 10 + 1.

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

Related questions