Mentor SAP

Generalization and Specialization

Specialization describes a relationship in which one class (subclass) inherits all the main characteristics of another class (superclass). The subclass can also add new components (attributes, methods, and so on) and replace the implementations in inherited methods. In the latter case, the method name in the Unified Modeling Language (UML) diagram is renamed within the subclass.

 

Specialization is an implementation relationship that emphasizes the similarities of the classes. In the example shown in the figure, the common components of classes LCL_CAR, LCL_TRUCK, and LCL_BUS are defined in the UML model in the superclass, LCL_VEHICLE. The common components of the subclasses only need to be defined and implemented in the superclass and they are inherited by all the subclasses.

 

Specialization is described as an "is a" relationship semantically. For example, a truck is a specific vehicle. Reversing the point of view is referred to as generalization.

 

 

Characteristics of Generalization and Specialization

Generalization and specialization provide a significantly better structure for your software because commonly used elements only need to be stored once in a central location (in the superclass). These elements are then automatically available to all subclasses. Changes made at a later stage have an immediate effect on the subclasses. Therefore, do not alter the semantics when you change a superclass.

 

You need knowledge of the implementation of the superclass to decide whether the inherited components from the superclass are sufficient for the subclass or if they must be extended. Generalization/specialization, therefore, provides strong links between the superclass and the subclass.

 

 

Implementation of Inheritance

In ABAP Objects, an inheritance relationship is defined for a subclass using the INHERITING FROM addition, followed by the superclass directly above the subclass. Usually, inheritance hierarchies of varying complexity can be created when a superclass inherits from another superclass, but there is no multiple inheritance in ABAP Objects, that is, only one superclass can be specified directly above a class. However, you can use interfaces in ABAP Objects to simulate multiple inheritance.

 

Inheritance must be used to implement generalization and specialization relationships. A superclass is a generalization of its subclasses. The subclasses are in turn different specializations of their superclasses. Thus, additions or changes are permitted in the subclasses but you can never remove anything from a superclass in a subclass.

 

Inheritance is a one-sided relationship, subclasses recognize their direct superclasses but superclasses do not recognize their subclasses. In this example, the subclass also contains the set_type method. The subclass also defines the get_cargo method.

 

 

Redefinition of Methods

Redefinition is when the implementation of an inherited instance method is changed for the subclass, without changing the signature. Because the visibility section for the superclass must remain the same, therefore, redefinition is not possible within the PRIVATE SECTION.

 

When you use the REDEFINITION addition, you will specify a new implementation for the inherited method. Since the signature may not be changed, you do not need to define the method parameters and exceptions again. Within the redefined implementation of the method, you can use the predefined prefix “super->...” to access components in the superclass. You may need to do this when redefining a method to call the original method of the superclass.

 

 

Preserving Semantics During Redefinition

In the figure the two redefined methods provide completely different information.

The semantics of the method remains the same.

 

Subclass Constructors

A redefinition, as described for the methods in the figure, would not be useful in the case of the constructor. In the subclass, either the constructor of the superclass can be used without any changes or the subclass has been expanded and the new  parameters are now required in the constructors signature.