super Keyword

AP Computer Science A· difficulty 3/5

public class A {
    public String label() { return "A"; }
    public String tag() { return label(); }
}
public class B extends A {
    public String label() { return "B"; }
}
// Usage:
B b = new B();
System.out.print(b.tag());

What is printed?

  • A

    Compile error: tag missing in B

  • B

    AB

  • C

    A

  • D

    B

    check_circle

Explanation

tag() is inherited and calls label() (this.label()). Dynamic dispatch invokes B's label(), printing "B".

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

Related questions