The three conflict rules in action
Since Java 8 an interface can contain default methods (with a body that implementers inherit) and static helper methods; since Java 9 it can also have private methods shared by those bodies. Default methods let old interfaces grow without breaking existing classes.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Create interface Greeter { String name(); default String greet() { ... } } that returns "Hello, <name>!". Implement it in English and in Hindi, where Hindi overrides greet() to return "Namaste, <name>!".
Create two interfaces Walker and Swimmer, each with a default move(). Write Frog implements Walker, Swimmer whose move() returns both results joined by " and ".
Write an interface Temperature with abstract double celsius(), a default fahrenheit(), and a static factory Temperature of(double c) that returns an object of a small implementing class in the same file.
Explain it without notes
Why were default methods added in Java 8?
State the three rules for resolving a method inherited from several places.
How do static interface methods differ from static class methods?
What problem do private interface methods solve?
Why can't a default method override toString()?
Expected output
camera ready + phone ready
device ready (class wins)
pro camera ready (more specific wins)