Interface
Last updated
Last updated
An Interface in UML and object-oriented programming is a contract that specifies a set of public methods that a Class must implement. In Modeldraw, you can use Interfaces to define common behavior that can be implemented by multiple Classes, promoting code reusability and defining clear contracts between different parts of your system.
An Interface represents a collection of abstract methods and (sometimes) constants that define a specific behavior. Key characteristics include:
It declares methods but doesn't implement them
A Class can implement multiple Interfaces
It establishes a contract that implementing Classes must fulfill
Interfaces support a form of multiple inheritance in languages that don't allow Class-based multiple inheritance
An Interface is represented in UML by a rectangle similar to a Class, but with the «interface» stereotype.
It's important to note that there are two common approaches to representing methods from implemented Interfaces in UML Class diagrams:
Omission: As shown in the example above, the methods defined in the Interface are not repeated in the implementing Classes. This approach reduces clutter and redundancy in the diagram, especially when dealing with multiple Interfaces or complex Class hierarchies.
Explicit listing: Alternatively, you might choose to list all methods in the implementing Classes, including those inherited from Interfaces. This approach provides a complete view of each Class's capabilities at a glance.
Both approaches are valid, and the choice often depends on:
The complexity of your diagram
The level of detail required
Your team's or organization's modeling guidelines
The specific purpose of the diagram (e.g., high-level overview vs. detailed design)
In Modeldraw, you have the flexibility to use either approach. If you choose to omit the Interface methods in the implementing Classes, the Realization relationship clearly shows that these methods are indeed part of the Class's contract.
To create a Realization between a Class and an Interface:
Select the Realization tool from the toolbox
Click on a Class that implements the Interface
Move above and click the Interface
To add an existing Interface to a diagram:
Open the diagram you want to add the Interface to
Drag and drop the Interface from the navigation tree into the diagram