Class DefaultMethodExamples
java.lang.Object
net.jrodolfo.java_evolution.java08.DefaultMethodExamples
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classImplementation that overrides a default method while keeping the others.static interfaceInterface with one abstract method and two default methods.static classImplementation that inherits all default behavior fromDefaultMethodExamples.Named. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a user that overrides one default method.regularUser(String name) Creates a user that relies on the default methods fromDefaultMethodExamples.Named.
-
Constructor Details
-
DefaultMethodExamples
public DefaultMethodExamples()
-
-
Method Details
-
regularUser
Creates a user that relies on the default methods fromDefaultMethodExamples.Named.- Parameters:
name- the user's name- Returns:
- a user implementation with no custom default-method overrides
-
adminUser
Creates a user that overrides one default method.- Parameters:
name- the user's name- Returns:
- a user implementation with a custom display name
-