Mentor SAP

Abstract Classes

Abstract class contains both definition and implementation but cannot be instantiated. Use the ABSTRACT addition in the CLASS statement to create an abstract class. Superclasses are a typical use for abstract classes, as they are not to be instantiated themselves, but their subclasses are.

 

In an abstract class, you can define abstract methods among other things. This means that the abstract method cannot be implemented in that class. Instead, it is implemented in a subclass of the class.  If the subclass of that class is not abstract, the abstract methods must be redefined and implemented in the subclass for the first time.

 

The relevant indicator is in the Class Builder on the Attributes tab page for that class or method.

 

References to such abstract classes can be used for polymorphic access to subclass instances. Static methods cannot be abstract because they cannot be redefined.

 

Final Classes

Prevent a class from being inherited by using the FINAL addition with the class statement. You can prevent a method from being redefined by using the FINAL addition with the methods statement.

 

The relevant indicator is in the Class Builder on the Attributes tab page for that class or method.

 

Thus, all methods of a final class are implicitly final. You may not repeat the FINAL addition in the methods themselves. Classes that are abstract and final should only contain static components.

 

Internal Tables with Object References

We have learned in a previous section that object references can be stored in internal tables. This is used, in particular, to implement associations. However, we have not yet discussed how we should retrieve a specific object from such a list. The figure gives an example of how this could be achieved.

 

Object references can be stored in internal tables.

 

The example in the figure explains how to retrieve a specific object from such a list. The object reference is stored in the table along with some key information. In the example, the key information is the make and model of the vehicle. The object reference can easily be retrieved through this key information. This technique implies redundant storage of information as the key values are already stored in attributes of the object.

 

Read Access Using Public Attributes

 

As shown in the figure, the key information redundancy can be avoided by making the key attributes public. Public attributes can be used directly in the statements to access internal tables like READ TABLE and LOOP AT. The expression TABE_LINE used here is a built-in part of the ABAP language. When the line type of an internal table is an elementary data type instead of a structure type, you need to use TABLE_LINE. Such internal tables have just one unnamed column. TABLE_LINE is used as the generic column name for this single column.

 

Hint: It is against the fundamental principles of object-oriented programming to make attributes public. Unfortunately, ABAP does not allow you to use functional methods (like the methods in our example GET_MAKE, GET_MODEL, and so on) on the left side of WHERE clauses and WITH KEY additions. Therefore, public attributes are only used in some special cases in read-only mode.

 

Navigation Methods and Chain Method Calls

Associations like aggregations and compositions are an important design principle of object-oriented programming. An association means that at runtime, an instance of one class stores references to objects of another class.

 

Classes that have other classes associated with them often come with methods that return one of the stored references to the caller. Such methods are often called navigation methods, as they can be used to navigate from one object to another. In the example, the LCL_VEHICLE class is associated with the LCL_RENTAL class. Class LCL_RENTAL provides the navigation method GET_VEHICLE.