Making use of private interface methods

Lambda expressions were a big part of the Java 8 release. As a follow-up to that improvement, private methods in interfaces are now feasible. Previously, we could not share data between non-abstract methods of an interface. With Java 9, this data sharing is possible. Interface methods can now be private. Let's look at some sample code.

This first code snippet is how we might code an interface in Java 8:

    . . .     public interface characterTravel    {      pubic default void walk()      {        Scanner scanner = new Scanner(System.in);        System.out.println("Enter desired pacing: ");        int p = scanner.nextInt();        p = p +1;      }      public default void run()      {        Scanner scanner = new Scanner(System.in); System.out.println("Enter desired ...

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.