oop
7. The Diamond Problem: Default Methods
Analyze the code, write your predicted output, and then reveal the solution.
Scenario
Java doesn't support multiple inheritance via classes. But with Java 8 default methods in interfaces, we can have a conflict. How does Java solve it?
Code Block
interface Camera { default void start() { System.out.println("Camera starting..."); } } interface GPS { default void start() { System.out.println("GPS starting..."); } } class SmartPhone implements Camera, GPS { @Override public void start() { // Must explicitly decide which one to call or provide new logic Camera.super.start(); GPS.super.start(); System.out.println("Phone ready!"); } } public class Main { public static void main(String[] args) { new SmartPhone().start(); } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.