Command Palette

Search for a command to run...

Back to the lesson: Topic 5.8 — Default, Static and Private Interface Methods
Core Java · Example 3 of 4 Java 9+

Private interface methods

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.

01

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>!".

02

Create two interfaces Walker and Swimmer, each with a default move(). Write Frog implements Walker, Swimmer whose move() returns both results joined by " and ".

03

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

01

Why were default methods added in Java 8?

02

State the three rules for resolving a method inherited from several places.

03

How do static interface methods differ from static class methods?

04

What problem do private interface methods solve?

05

Why can't a default method override toString()?

Private interface methods Java 9+
Sign in to run this example in your browser.

Expected output

[SALES Q3]
[# Sales Q3]
Sales Q3 (12 pages)
Office closed Friday (1 page)