super Keyword

AP Computer Science A· difficulty 3/5

public class A {
    public String hello() { return "Hi from A"; }
}
public class B extends A {
    public String hello() { return super.hello() + " and B"; }
}
System.out.println(new B().hello());

What is printed?

  • A

    Stack overflow error

  • B

    and B

  • C

    Hi from A and B

    check_circle
  • D

    Hi from A

Explanation

<code>super.hello()</code> calls A's version, returning "Hi from A". Then " and B" is appended.

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

Related questions