oop
16. Interface Evolution: Private & Static Methods
Analyze the code, write your predicted output, and then reveal the solution.
Scenario
Interfaces are no longer just for abstract methods. Since Java 8 and 9, they can have logic. This demonstrates how to use static and private methods in interfaces.
Code Block
interface Logger { default void logInfo(String msg) { log(msg, "INFO"); } default void logError(String msg) { log(msg, "ERROR"); } // Private method for internal logic (Java 9+) private void log(String msg, String level) { System.out.println("[" + level + "] " + msg); } // Static method for utility (Java 8+) static void printHeader() { System.out.println("=== SYSTEM LOGS ==="); } } class ConsoleLogger implements Logger {} public class Main { public static void main(String[] args) { Logger.printHeader(); // Called on Interface ConsoleLogger cl = new ConsoleLogger(); cl.logInfo("System Started"); cl.logError("Memory Low"); } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.