| Local classes |
|
| Global classes |
|
PUBLIC SECTION. |
Components declared in this section can be accessed from within the class and from all users of the class. |
PROTECTED SECTION. |
Components declared in this section can be accessed from within the class and subclasses as well as friends - concepts related to inheritance. |
PRIVATE SECTION. |
Components declared in this section can only be accessed from within the class in which they are declared and its friends. |
| Syntax Example | Details |
| ``` abap CLASS zcl_demo DEFINITION PUBLIC FINAL CREATE PUBLIC . ``` |
- `... PUBLIC ...`: Specifies that the class is a global class, available globally within the class library. Most of the subsequent snippets use the `PUBLIC` addition as the focus is on global classes. - `... FINAL ...`: Specifies that the class cannot have any subclasses, effectively prohibiting inheritance. This addition seals off a branch of the inheritance tree. In final classes, all methods are automatically final. - `... CREATE PUBLIC ...`: Specifies that the class can be instantiated wherever it is visible. |
| ``` abap CLASS zcl_demo DEFINITION PUBLIC CREATE PUBLIC . ``` |
This class permits inheritance because it does not include the `FINAL` addition. Subclasses can be derived from this superclass. |
| ``` abap CLASS zcl_demo DEFINITION PUBLIC CREATE PROTECTED . ``` |
- This class permits inheritance because it does not include the `FINAL` addition. - `... CREATE PROTECTED ...`: Specifies that the class can only be instantiated within its own methods, its subclasses' methods, or those of its friends. |
| ``` abap CLASS zcl_demo DEFINITION FINAL CREATE PRIVATE . ``` |
- This class does not permit inheritance because it includes the `FINAL` addition. - `... CREATE PRIVATE ...`: Specifies that the class can only be instantiated within its own methods or those of its friends. It cannot be instantiated as a component of (not befriended) subclasses. - Consider the implications of defining a superclass in this manner: - External users cannot instantiate a subclass. - If inheritance is allowed, subclasses cannot instantiate themselves though. This is because they cannot access the superclass's instance constructor, preventing the creation of subclass instances. However, this can be altered if the subclass is a friend of the superclass. - Similarly, subclass objects cannot be created within their superclass if declared using `CREATE PROTECTED` or `CREATE PRIVATE`. This is only possible if the superclasses are friends with their subclasses. - The `FINAL` addition can be beneficial with `CREATE PRIVATE` to prevent the derivation of subclasses. - Note: In each class, the `CREATE PUBLIC`, `CREATE PROTECTED`, and `CREATE PRIVATE` additions of the `CLASS` statement control who can create an instance of the class and who can call its instance constructor. |
| ``` abap CLASS zcl_demo DEFINITION PUBLIC ABSTRACT CREATE ... ``` |
- `... ABSTRACT ...`: - Defines abstract classes - Abstract classes cannot be instantiated. - To use the instance components of an abstract class, you can instantiate a subclass of that class. - Abstract classes may contain both abstract and concrete instance methods. However, abstract methods are not implemented within abstract classes. - By adding the `FINAL` addition, abstract classes can be made final. In these cases, only static components are usable. While instance components may be declared, they are not usable. |
| ``` abap CLASS zcl_sub DEFINITION INHERITING FROM zcl_demo ... ``` |
- `... INHERITING FROM ...`: - Can be specified in subclasses inheriting from visible superclasses - If not specified, the class implicitly inherits from the predefined, empty, abstract class `OBJECT` (the root object). - A subclass inherits all components of the superclasses. - For example, if an internal table `itab` is declared as a static component in superclass `zcl_super`, subclasses can refer to `itab` directly, not necessarily by specifying the class name as with `zcl_super=>itab` (which is also possible). - If the superclass defines a type, subclasses cannot define a type with the same name. - The visibility of the components remains unchanged. - Only the public and protected components of the superclass are visible in the subclass. - Private components of superclasses are inaccessible in subclasses. - The properties of inherited components are immutable. However, subclasses can declare additional components (with unique names) and redefine inherited methods without altering the interface. - Upon instantiation of a subclass, all superclasses are also instantiated, ensuring the initialization of superclass attributes through calling superclass constructors. |
| Syntax Example | Details |
| ``` abap METHODS some_meth FINAL ... ``` |
- Declares final methods - These methods cannot be overridden in subclasses. - Note: In final classes, all methods are inherently final. Therefore, the `FINAL` addition cannot be specified. Instance constructors are always final, but the use of the `FINAL` addition is optional. |
| ``` abap METHODS some_meth ABSTRACT ... ``` |
- Declares abstract methods - Can only be used in abstract classes - You can implement these methods in a subclass (by redefining using the `REDEFINITION`addition), not in the abstract class itself. When declared, there is no implementation part in the abstract class. - All instance methods can be declared as abstract, except for instance constructors. - Private methods cannot be redefined and can therefore not be declared as abstract. - If abstract methods are declared in classes that are both abstract and final, they cannot be implemented. Therefore, the methods are not usable. - In interfaces, methods are implicitly abstract as interfaces do not contain method implementations. |
| ``` abap "Instance constructor METHODS constructor. "Static constructor CLASS-METHODS class_constructor. ``` |
- Instance constructors (`constructor`): - Subclasses cannot redefine the instance constructors of superclasses. - These constructors are automatically invoked when creating an object, not explicitly called. - In inheritance, a subclass's instance constructor must call the instance constructors of all its superclasses. This requires calling `super->constructor( ).` to call the instance constructor of the direct superclass, even if the superclass does not explicitly declare the instance constructor. Note that any non-optional importing parameters must be filled. - Static constructors (`class_constructor`): - These constructors are implicitly available in all classes, whether declared or not. - They are called when creating a class instance for the first time in an ABAP program or when addressing a static component, excluding types and constants. - In inheritance, the static constructors of all classes up the inheritance tree are called first. - A static constructor can only be called once during program runtime. |
| ``` abap METHODS some_meth REDEFINITION. METHODS another_meth FINAL REDEFINITION. ``` |
- Specified in subclasses to redefine inherited methods from superclasses. - The method's implementation is expected to reimplement the inherited method. However, the subclass's new implementation conceals the superclass's implementation. - The redefined method accesses the private components of its class, not any similarly named private components in the superclass. - The superclass's implementation can be called in the redefined method using the [pseudo reference](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenpseudo_reference_glosry.htm "Glossary Entry") `super->meth( ).`. Note that non-optional importing parameters must be filled. - The redefinition is valid for subclasses until the method is redefined again. - The `FINAL` addition can be specified, preventing further redefinition of the method in other subclasses. |