← Quiz: SOLID in Java: all 10 questions
Quiz: SOLID in Java · Question 7 of 10
What does this print? (code)
What does this print?
interface Notifier { String send(String m); }
class Sms implements Notifier {
public String send(String m) { return "SMS:" + m; }
}
class Email implements Notifier {
public String send(String m) { return "MAIL:" + m; }
}
class Service {
private final Notifier n;
Service(Notifier n) { this.n = n; }
String run() { return n.send("hi"); }
}
public class Main {
public static void main(String[] args) {
System.out.println(new Service(new Email()).run() + " " + new Service(new Sms()).run());
}
}
- A.SMS:hi MAIL:hi
- B.MAIL:hi SMS:hi
- C.MAIL:hi MAIL:hi
- D.hi hi
Answer & explanation
Correct answer: MAIL:hi SMS:hi
The same Service class behaves differently depending on which Notifier is injected.
Can you get the rest right without peeking?
Take the timed Quiz: SOLID in Java quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →