Different Between Operation & Method in Class Diagrams


In class diagrams, operations and methods are both used to describe the behavior of a class, but they have some differences. Here is a table that summarizes the differences:

Operation Method
Represents a behavior or action that can be performed on an object of the class. Represents a function or procedure that is associated with the class.
May or may not return a value. Always returns a value.
May or may not take parameters. May or may not take parameters.
Typically indicated in the class diagram by listing the name of the operation followed by the parameter types and return type (if any) in parentheses. Typically indicated in the class diagram by listing the name of the method followed by the parameter types and return type (if any) in parentheses, and underlined.

Here is an example of how an operation and a method might be represented in a class diagram:

+------------+             +-------------------+
|   MyClass  |             |     AnotherClass  |
+------------+             +-------------------+
| - myVar    |             | - anotherVar      |
+------------+             +-------------------+
| + doSomething(param1: int, param2: String): void |   // Operation
| + calculateSomething(): double                    |   // Method
+------------+             +-------------------+

In this example, doSomething is an operation of MyClass, while calculateSomething is a method of MyClass. The parameters and return types are indicated in parentheses for both. The method is underlined to indicate that it is a method.

       

Advertisements

ads