100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Role Interface in OOP?

Role interfaces explained — small, client-focused contracts vs header interfaces, with a Java example implementing multiple roles.

mediumQ196 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A role interface is a small, focused interface that describes one specific behavior or capability a client depends on, rather than the full public surface of a class, letting a single class implement multiple narrow role interfaces instead of one large header interface.

The term, coined by Martin Fowler, contrasts with a “header interface” that mirrors an entire class’s public API. Role interfaces are defined from the client’s point of view: instead of asking “what does this class do overall,” you ask “what does this particular caller actually need from it,” and design an interface around just that. A class like FileStream might implement Readable, Writable, and Closeable as separate role interfaces, so a method that only reads never has to depend on write or close capabilities. This is the practical technique for satisfying the Interface Segregation Principle: role interfaces are how you deliberately design cohesive contracts up front, avoiding both fat interfaces and interface pollution as the codebase grows.

  • Clients depend only on the capability they actually use
  • A single class can freely implement several role interfaces
  • Reduces coupling between unrelated callers of the same class
  • Makes mocking and testing far more targeted and simple

AI Mentor Explanation

Instead of one giant "Player" contract, a team defines separate role contracts — Batsman, Bowler, Fielder, WicketKeeper — and an all-rounder simply implements several of them at once, while a specialist bowler implements only Bowler and Fielder. Each contract describes one focused capability from the perspective of what a specific situation needs. That is a role interface: a narrow, purpose-built contract designed around one capability rather than an entire player’s full description.

Step-by-Step Explanation

  1. Step 1

    Start from the client, not the class

    Identify what a specific caller actually needs from an object, rather than the object’s full API.

  2. Step 2

    Name the capability

    Define a small interface around exactly that capability (e.g. Readable, Closeable).

  3. Step 3

    Let classes implement multiple role interfaces

    A single concrete class can implement several role interfaces to expose its full behavior piecewise.

  4. Step 4

    Have callers depend on the narrow role

    Method signatures accept the specific role interface needed, not the concrete class or a fat interface.

What Interviewer Expects

  • Correct attribution/definition: client-focused, capability-scoped interface (Fowler’s term)
  • Contrast with a “header interface” that mirrors a whole class’s API
  • Concrete example of one class implementing multiple role interfaces
  • Connection to the Interface Segregation Principle as the guiding principle

Common Mistakes

  • Confusing a role interface with an abstract class
  • Not contrasting it with a header interface
  • Thinking a class can only implement one role interface at a time
  • Describing it vaguely without a concrete capability-scoped example

Best Answer (HR Friendly)

A role interface is a small interface built around one specific thing a caller needs from an object, instead of listing everything that object can do. A single class can implement several of these focused interfaces at once, so different callers only depend on the exact capability they use. It is the practical way teams design around the Interface Segregation Principle from the start, rather than fixing a bloated interface later.

Code Example

One class implementing multiple role interfaces
interface Readable { String read(); }
interface Writable { void write(String data); }
interface Closeable { void close(); }

// FileStream implements each role interface separately
class FileStream implements Readable, Writable, Closeable {
    public String read() { return "file contents"; }
    public void write(String data) { System.out.println("Writing: " + data); }
    public void close() { System.out.println("Stream closed"); }
}

class LogPrinter {
    // Depends only on the Readable role, not the whole FileStream
    void printContents(Readable source) {
        System.out.println(source.read());
    }
}

FileStream stream = new FileStream();
new LogPrinter().printContents(stream); // only Readable is required here

Follow-up Questions

  • Who coined the term “role interface” and how does it differ from a header interface?
  • How does a role interface help satisfy the Interface Segregation Principle?
  • Can a single class implement multiple role interfaces at once? Give an example.
  • How would you refactor a fat interface into a set of role interfaces?

MCQ Practice

1. A role interface is best described as?

Role interfaces are narrow, client-focused contracts designed around a single capability, not the full class surface.

2. A role interface is contrasted with which term?

A header interface mirrors an entire class’s public API; a role interface is scoped to one capability instead.

3. Can a single concrete class implement multiple role interfaces?

A class routinely implements several role interfaces at once, each exposing one facet of its behavior to different callers.

Flash Cards

Role interface in one line?A small interface scoped to one specific capability a client needs, not a class’s full API.

Contrasted with?A header interface, which mirrors an entire class’s public surface.

Who coined the term?Martin Fowler.

Which principle does it directly support?The Interface Segregation Principle.

1 / 4

Continue Learning