Class DefaultMethodExamples

java.lang.Object
net.jrodolfo.java_evolution.java08.DefaultMethodExamples

public class DefaultMethodExamples extends Object
Demonstrates default methods, introduced in Java 8 so interfaces could evolve without forcing every existing implementation to add new methods immediately.

Before Java 8, adding a method to an interface was a breaking change for every implementation. That was a serious problem for old, widely used JDK interfaces because the platform needed to add new behavior while preserving compatibility with existing code.

Default methods solve this by allowing an interface to provide an inherited method body. Implementations can accept the default behavior or override it when they need something more specific.

  • Constructor Details

    • DefaultMethodExamples

      public DefaultMethodExamples()
  • Method Details

    • regularUser

      public DefaultMethodExamples.Named regularUser(String name)
      Creates a user that relies on the default methods from DefaultMethodExamples.Named.
      Parameters:
      name - the user's name
      Returns:
      a user implementation with no custom default-method overrides
    • adminUser

      public DefaultMethodExamples.Named adminUser(String name)
      Creates a user that overrides one default method.
      Parameters:
      name - the user's name
      Returns:
      a user implementation with a custom display name