UML Interface

In UML, an interface is a classifier that represents a contract within a system's design. An interface specifies a group of operations but does not implement them—this implementation is left to classes or other interfaces that realize or inherit from this interface.

What is a UML Interface?

An interface in UML is denoted similarly to a class; it is represented as a rectangle divided into compartments for the name, attributes (optional), and operations. The key distinction in UML is that an interface is annotated with the «interface» stereotype.

UML Interface Notation
UML Interface Notation

Key Concepts and Usage

  • Multiple Implementations: A single class can implement multiple interfaces, showcasing its capability to participate in several contracts, providing flexibility and promoting reusability. This also allows for a form of multiple inheritances in programming languages that do not support this feature directly, like C# or Java.
  • Polymorphism: Interfaces facilitate polymorphism. Different classes can be treated the same way if they implement the same interface, even if they have different internal implementations.

Interface Realization

Classes connect to interfaces using a realization relationship, depicted as a dashed line with a triangular arrowhead pointing towards the interface. This illustrates that the class agrees to implement the specified operations of the interface.

Interface Inheritance

When an interface needs to inherit from another, a generalization relationship is used (similar to class inheritance). This is represented by a solid line with a triangular arrowhead pointing towards the generalized interface. It is crucial to note that we do not use realization relationships for interface inheritance. Instead, we employ generalization to ensure notation consistency.

Generalization in interface inheritance allows us to abstract common functionality into a superinterface, which can then be extended or implemented by other interfaces. This hierarchy simplifies modifications and extensions as the system evolves.

UML Interfaces - Realization and Generalization
UML Interfaces - Realization and Generalization

Notations in UML

  • Detailed Notation: Shows all components of the interface, including name, operations, and attributes if applicable.
  • Lollipop Notation: Simplified representation using a circle (lollipop) connected to a class. This is particularly useful for simplifying complex diagrams while highlighting interface implementations.

Required vs. Provided Interfaces:

  • Provided Interfaces are shown as circles (lollipops) directly connected to the class. . The same concept can also be modeled using a detailed UML Interface connected to a class via a realization relationship. In this case, the arrow points from the class to the interface.
  • Required Interfaces can be represented as arcs in lollipop notation or using a use relationship in detailed notation.

UML Interface Implementation

  • Drag the provided or required interface tool from the toolbox and drop it onto the class element to designate the type of interface. The interface will automatically connect to the border of the selected class at an available space surrounding the class.
  • To specify details of interfaces, use the Interface tool to add an interface to the diagram and define its operations. Then, right-click on the interface and select "Properties..." from the context menu. In the "Interface" tab, click on the ellipsis button next to the Interface field to open the project tree dialog. Here, you can select the interface element that you wish to link.
Add provided and required interfaces to a class
Add provided and required interfaces to a class

Interface Context Bar Actions

  • Implement All Interfaces: Select a class and click this button to automatically add all operations from connected interfaces.
  • Implement Interface: Select a connector to add operations from the connected interface to the class.

UML Interface Example

In our example, we explore a smart home system managed through various sensor interfaces.

This hierarchical structure of interfaces, utilizing inheritance and specialization, allows for a modular and extensible design.

UML Interface Example
UML Interface Example

New Comment

Comment