← Playground
Java
90 seconds

Interfaces, without the textbook

Most people memorise "an interface has no method bodies." Let's make it stick instead. Build a valid interface below — catch the pure declarations, dodge the ones sneaking in a { body }.
0 / 5 compiled
interface Repository {
// tap the pure declarations below…
}

Tap only the methods that are legal inside an interface — declarations with no body.

void save(User user);
void deleteById(int id);
List<User> findAll();
User cache() { return last; }
boolean exists(int id);
void log() { print("saved"); }
int count() { return db.size(); }
User findById(int id);
The one-line takeaway

An interface is a promise about what an object can do — never how. Its methods are declarations that end in ; with no body. The moment you add { … }, that's implementation — and that lives in a class that says implements Repository.


So where does the code actually go?

The interface is the contract. The class that implements it is where the real work — the method body — lives.

interface Repository {
    void save(User user);      // what, not how
    User findById(int id);
}

class SqlRepository implements Repository {
    public void save(User user) {   // the body lives here
        db.insert(user);
    }
    public User findById(int id) {
        return db.query(id);
    }
}
Enjoyed this?

I build mobile & full-stack apps freelance and like turning hard ideas into simple ones. Have a project — or a concept you wish someone would explain like this?