← Quiz: Abstract Classes and Interfaces: all 10 questions
Quiz: Abstract Classes and Interfaces · Question 8 of 10
What does this print? (code)
What does this print?
interface A { default String hi() { return "A"; } }
interface B { default String hi() { return "B"; } }
class C implements A, B {
public String hi() { return A.super.hi() + B.super.hi(); }
}
public class Main {
public static void main(String[] args) {
System.out.println(new C().hi());
}
}
- A.A
- B.B
- C.Compile error: two default methods clash
- D.AB
Answer & explanation
Correct answer: AB
When two interfaces give the same default method, the class must override it. It can call each version with InterfaceName.super.method().
Can you get the rest right without peeking?
Take the timed Quiz: Abstract Classes and Interfaces quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →