super Keyword

AP Computer Science A· difficulty 3/5

public class A {
    public String greet() { return "hello"; }
}
public class B extends A {
    public String greet() { return super.greet() + " world"; }
}
// Usage:
B b = new B();
System.out.print(b.greet());

What is printed?

  • A

    world

  • B

    hello hello world

  • C

    hello world

    check_circle
  • D

    Stack overflow

Explanation

super.greet() calls A's greet returning "hello". B's greet appends " world", so "hello world" is printed.

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

Related questions