inheritanceumlcode-reuseliskov-substitution-principlegeneralization

UML generalization without polymorphism


Is it possible to have a generalization relationship between two classes in which the subclass adds new operations that are not on the superclass (not overriding, not overloading)? It is not a LSP violation? I don't want polymorphism, just reuse of code in subclasses.

EDIT: the superclass is abstract, partially defined


Solution

  • Yes, UML allows defining new operations in a subclass that are not present in the superclass.

    This is not necessarily a violation of the Liskov Substitution Principle. Instances of the subclass can be substituted where instances of the superclass are expected (but you don't have to exploit this).

    However, make sure that instances of your subclass can really be considered instances of the superclass as well. If this is not the case and you just want to reuse code, the generalization is not the right solution.

    Example: Class StringCollection has an operation sort. Now, you want to reuse this sort operation in a new class called FileManager. FileManager is not just a StringCollection, so you should not use a generalization. Instead, you could use a ≪use≫-dependency (FileManager uses StringCollection) or a composition (FileManager contains StringCollection).