Skip to content

UML Interface

An interface in UML (the Unified Modeling Language) is a contract. It names operations that something must offer. It does not say how they work. You cannot create an object directly from an interface. A class or component has to realize it, meaning it supplies the real behavior. In Java, a class uses the implements keyword. In C#, the class lists the interface after a colon in its declaration.

Interface versus abstract class. An abstract class can share state between related types. A C# interface cannot hold instance fields or constructors, and its members are public by default. A class can implement several interfaces but inherit from only one base class. So abstract classes suit related types that share state. Interfaces suit contracts that span unrelated hierarchies.

Two ways to draw it. The explicit form is a class box marked with the «interface» stereotype. The compact form is a lollipop. A ball shows an interface that something provides. UML 2 added the socket to show an interface that something requires. Microsoft popularized the ball. The article Provided and Required Interface covers this in more depth.

Component diagrams. In a component diagram, the provided interface appears as a ball. Ball-and-socket is an accepted way to present interfaces there. One expert notes that mating a ball with a socket was ruled an error outside composite structure connectors. UML 1 used a dependency instead.

Why architects care. A black box exposes only its responsibility and its input and output interfaces. A user needs nothing more. The inner workings can change without clients having to adapt. This is information hiding.

A shop system defines an interface PaymentService with one operation, charge. Two classes, CardPayment and InvoicePayment, each realize it. The checkout component depends only on PaymentService. In the diagram, the payment components show a ball for PaymentService, and checkout shows a socket for it.

Interfaces cannot hold instance fields or constructors in C#. If related types need shared state, an abstract class fits better. A class can inherit only one base class, but it can implement many interfaces. The ball-and-socket shorthand is compact, but it hides the operation list, so use the class box when readers need the details.

One interface, realized by two classes and used by a client.

Diagram source
@startuml
interface PaymentService {
+charge()
}
class CardPayment
class InvoicePayment
class Checkout
CardPayment ..|> PaymentService
InvoicePayment ..|> PaymentService
Checkout ..> PaymentService : uses
@enduml
  • Each interface shown in a component diagram is drawn as a ball (provided) or socket (required), or as a class box with the interface stereotype.
  • Black-box building blocks describe their responsibility and their interfaces, not their internals.
  • No interface is shown as instantiated; each is realized by at least one class or component.