Search code examples
interfaceclass-designumlclass-diagram

class versus interface in uml


As we know in OOP that interface provides a set of operations without implementation but class is the opposite.

in Object oriented design ,we use uml the interface has a set of operations without implementation and the class also has a set of operations without implementation(i know class has attributes in addition to its operations)?

so, what is the difference in UML?


Solution

  • As we know in OOP that interface provides a set of operations without implementation but class is the opposite.

    Not quite true - abstract classes are classes that have one or more methods declared but not defined (in C++ and Java these are abstract methods). You can have a class defined with all its methods abstract - in which case there is close similarity with an interface.

    One key idea in UML, though, is that an interface is a set of methods exposed to other classes or components. The purpose is to define a set of operations.

    However, moving to programming, a method may be made abstract to aid development (e.g. by ensuring all subclasses have an implementation). This method might be purely internal to the class.

    One last observation: the term interface and class in UML are not quite synonymous to interface and class in a language, say Java. For example, Java does not allow multiple class inheritance. Instead Java has the interface which allows a class to implement multiple types (not classes - a subtle difference)

    EDIT

    Quick note technical words:

    • Declare: Stating to the system that a variable or operation exists and its type or signature
    • Define: Same as declaring, but additionally providing a complete implementation of a variable or operation
    • Interface: A set of declarations of operations
    • Type: An object's interface(s) and other operations
    • Class: An object's class defines (not declares) how the object is implemented, including its internal state and the implementation of its operations

    Define is to Declare as Class is to Type.
    (see What is the difference between Type and Class?)

    The purpose of interface is to define a set of operations but we are do the same for class also define a set of operations?

    So the purpose of the interface is to declare (not define) a set of public operations that other objects want to use. A class (in UML) is the complete set of operations (public and private). A class (in Java, C++, etc.) additionally defines all non-abstract operations.

    So the key is the intent: When other components of the system want to use a set of operations, use interface. When you're using UML to describe an implementation (of a component, algorithm, etc.) use class.

    when I go to class that assumed to implement those operations I can't see any implementation for those operations as a diagram describe those operations or anything give a sign for implementation?

    UML tool is for modelling and so deliberately avoids providing a place where you enter operation definitions - that is left for later. The idea is that you:

    • Define the model in UML
    • Use the UML tool to generate code in the target language
    • (And some allow you to import your code back into the tool to modify the model with any changes made during implementation. This is called "round-trip" modelling, something which the old TogetherJ product excelled at)

    This deliberate gap (you might say deficiency) means that 'define' vs. 'declare' in UML is meaningless. Sorry.