How to present abstract class which has relationship with interface class in class diagram

Viewed 568

I have to do diagram with minimum of one abstract class and interface.I though to be a good idea to implement the interface class here. My table is customer which is the abstract class.The interface class shows the methods that needs to be included in the two types of customers.My question is,is this how I present an abstract class which is connected with interface class. Should I leave the customer class blank? Is it wrong to use interface with abstract class ?

This is my diagram:

enter image description here

2 Answers

Since your question seems about UML modelling, I'll provide some additional, more UML oriented information:

  • Your «interface» defines a contract with three operations (in java operations are methods). This means that all classes that implement this interface must provide the three operations.
  • According to your narrative, the abstract class Customer implements the interface. This shall be represented in the diagram with "realization" dependency, i.e. a doted line from the class to the interface with a plain blank arrow-head. (Caution: Your diagram seems to present a navigable association, and this would have a completely different meaning)
  • But realization is not inheritance : nothing happens by default with a realization relationship and you have to explicitly add all three operations for Customer. If some operations remain abstract at this level, they should be marked as such (i.e. in italic). You can redefine any of those abstract operations in specializations (i.e. inherited classes) of Customer.

This being said, the abstract class makes only sense if it adds some value, such as providing a default implementation for some methods, add some additional methods or properties. If it is just an empty box, and child classes have anyway to implement everything, then you can remove the abstract class from your design:

  • Any class inheriting the abstract class would then just realize the interface instead
  • Any class associated with the abstract class would then be be associated with the interface, which would stand in for any class that implements it.

interface is not a class. Given below is a definition from Oracle's tutorial:

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

The same page mentions,

There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts.

Thus, in the interface, you should put the method signatures (contracts) which you want all implementing classes should mandatorily have.

Your abstract class can implement some of these methods and these implementations serve as the default implementation for the child classes (i.e. the classes which extend the abstract class). This class can have additional members for its child classes to be inherited.

Related