GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Interfaces — java.evolved

Home / Topics / Interfaces
🏷 Interfaces 4 patterns

Topic: Interfaces

All Java patterns related to Interfaces — java.evolved

Language

Private interface methods

Old interface Logger { default void logInfo(String msg) { System.out.println( "[INFO] " + timestamp() + msg); } default void logWarn(String msg) { System.out.println( "[WARN] " + timestamp() + msg); } }
Modern interface Logger { private String format(String lvl, String msg) { return "[" + lvl + "] " + timestamp() + msg; } default void logInfo(String msg) { IO.println(format("INFO", msg)); } default void logWarn(String msg) { IO.println(format("WARN", msg)); } }
hover to see modern →
JDK 9+ learn more →
Language

Anonymous classes to lambdas and method references

Old button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { save(event); } });
Modern button.addActionListener(this::save);
hover to see modern →
JDK 8+ learn more →
Language

Default interface methods

Old // Need abstract class to share behavior public abstract class AbstractLogger { public void log(String msg) { System.out.println( timestamp() + ": " + msg); } abstract String timestamp(); } // Single inheritance only public class FileLogger extends AbstractLogger { ... }
Modern public interface Logger { default void log(String msg) { IO.println( timestamp() + ": " + msg); } String timestamp(); } // Multiple interfaces allowed public class FileLogger implements Logger, Closeable { ... }
hover to see modern →
JDK 8+ learn more →
Language

Static methods in interfaces

Old // Separate utility class needed public class ValidatorUtils { public static boolean isBlank( String s) { return s == null || s.trim().isEmpty(); } } // Usage if (ValidatorUtils.isBlank(input)) { ... }
Modern public interface Validator { boolean validate(String s); static boolean isBlank(String s) { return s == null || s.trim().isEmpty(); } } // Usage if (Validator.isBlank(input)) { ... }
hover to see modern →
JDK 8+ learn more →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.