This is a test message to test the length of the message box.
Login
ABAP OO Classes and Inheritance
Created by Software-Heroes

ABAP OO - Class and Inheritance

In the next part of the ABAP OO basics we want to show you the basic concept of the class and its characteristics, as well as the inheritance between the individual objects.

Advertising

In the last article we showed you why you should use classes and what the special features of ABAP OO in SAP are. In today's article we mainly want to look at how a class is structured and what can be achieved with it. We'll also look at the first simple inheritance.

 

Introduction

Why is it now necessary to know all the components of a class and to be able to define them yourself? Over the years ABAP OO was mainly distributed via the form editor (SE24/SE80), so the system has already taken over many steps of a class definition for you and you no longer had to worry about it. With Eclipse this is now again the responsibility of the developer, as there is only one text-based editor and you can edit the whole class at a glance.

Hint: We also clearly recommend working with Eclipse, as this gives you an overview of the entire class, which offers you a significantly higher development speed, as you save yourself having to navigate through the individual views and have everything at a glance.

 

Class

The class definition forms the shell of a class, in it you can create various objects that you can only use within the class, depending on their visibility, or that you can also make available for objects outside the class. The simplest form of a class therefore looks like this:

CLASS lcl_example DEFINITION.
ENDCLASS.

 

CLASS and ENDCLASS form the beginning and the end of the class. After CLASS comes the name of the class under which it can be addressed. Then comes the DEFINITION, which indicates that the class definition takes place here. In ABAP, the definition of a class and the actual implementation are always separate from one another. The implementation would then look like this:

CLASS lcl_example IMPLEMENTATION.
ENDCLASS.

 

If you then want to define methods or attributes, you first have to define the visibility for them. You can find out more about visibility at the end of the article. To make things easier, let's first define everything in the public area so that it can also be used outside of the class.

CLASS lcl_example DEFINITION.
  PUBLIC SECTION.
    DATA:
      md_status TYPE i,
      md_text   TYPE string.

    METHODS:
      get_final_text
        RETURNING VALUE(rd_result) TYPE string.
ENDCLASS.

CLASS lcl_example IMPLEMENTATION.
  METHOD get_final_text.
    rd_result = |{ md_text }: { md_status }|.
  ENDMETHOD.
ENDCLASS.

 

Only the individual methods with their code belong in the implementation of the class, which simplifies the clarity and separates the interface and data from the actual logic. In many other programming languages there is only one place/definition of the class for this.

 

Class properties

When defining a class, various properties can also be defined which have corresponding effects on the handling of the class. We want to briefly list these properties in this section. Properties that are not yet listed here, are following in further sections or in other articles, where we want to go into detail again.

 

FINAL

A class defined with FINAL has inheritance restrictions; inheritance from this class is no longer permitted. It thus forms the end of the chain, for example because it has implementations that are too special or because subclasses with different logic are to be prevented from being implemented.

CLASS lcl_example DEFINITION FINAL.
ENDCLASS.

 

CREATE

The addition restricts who can execute the constructor and thus the instantiation of the object. A public creation is normal and will be taken as the standard if not specified. In this case we set the creation to private, so that only the class itself or one of the defined friends can create an instance.

CLASS lcl_example DEFINITION CREATE PRIVATE.
ENDCLASS.

 

FRIENDS

Defines a list of classes and interfaces that can also access protected and private methods/attributes from outside. This concept nullifies the principle of visibility and should only be used with caution and if you know what you are doing.

CLASS lcl_example DEFINITION FRIENDS lcl_other_class.
ENDCLASS.

 

Hint: We will examine the concept of friends in detail again in a later article, as it is a special feature of ABAP OO.

 

Global/Local

Another important distinction is the use of the class and where you define it. There are local classes that are defined within a report, a function group or a global class. For example, we would start such classes with "LCL_" (Locale class) and real global classes that start with "ZCL_" (Class). There are therefore a few small differences in the definition, as well as a comparison.

CLASS zcl_example DEFINITION PUBLIC FINAL.
ENDCLASS.

CLASS lcl_example DEFINITION FINAL.
ENDCLASS.

 

The global class is given the addition PUBLIC and thus defines it as a global class. A global class is known throughout the system and can be used, as can its components that have been defined in the public area.

 

Visibility

Visibility becomes important when someone wants to interact with your class. When it comes to class visibility, there are three cases:

  • Public - Objects in this area are accessible to everyone from the outside and can be used. A public variable can therefore be read and changed directly.
  • Protected - The protected area behaves like the private area, but is also made available to the inheriting class in the event of inheritance.
  • Private - The object cannot be accessed from the outside and can only be used within the current class. These are mostly variables and methods that subdivide the public methods or act as helper methods.

 

Within the class there are therefore the individual areas that must be defined before you can assign methods and attributes. The areas are defined as follows:

CLASS lcl_example DEFINITION.
  PUBLIC SECTION.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

 

The visibility influences the inheritance and the use of the class. In these three areas there can be attributes, methods, types and constants for which exactly these rules apply. When you create an object, you normally only use public and private, if you want to inherit properties, these are usually set from private to protected.

 

Inheritance

Inheritance ensures that classes can be reused and expanded within a defined framework. This allows you to specialize a class further and implement new features. With inheritance, visibility also has a big impact on what is passed to the subclass. Therefore, you should keep in mind that public and protected properties are passed on to the inheriting class.

For this we define a local class LCL_ANIMAL which should represent an abstract animal. The number of arms and legs is saved internally and there is a method as an interface to determine the values of the animal.

CLASS lcl_animal DEFINITION.
  PUBLIC SECTION.
    METHODS:
      get_number_of_arms
        RETURNING VALUE(rd_result) TYPE i,

      get_number_of_legs
        RETURNING VALUE(rd_result) TYPE i.
    
  PROTECTED SECTION.
    DATA:
      md_arms TYPE i,
      md_legs TYPE i.

  PRIVATE SECTION.
ENDCLASS.

CLASS lcl_animal IMPLEMENTATION.
  METHOD get_number_of_arms.
    rd_result = md_arms.
  ENDMETHOD.

  METHOD get_number_of_legs.
    rd_result = md_legs.
  ENDMETHOD.
ENDCLASS.

 

We have created the attributes in the protected area so that no one can access them from outside or change the content. Now we want to define a class that uses this template and implements a specified animal.

CLASS lcl_cat DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
    METHODS:
      constructor.
ENDCLASS.

CLASS lcl_animal IMPLEMENTATION.
  METHOD constructor.
    md_legs = 4.
  ENDMETHOD.
ENDCLASS.

 

With INHERITING FROM you say during the class definition that you want to inherit from another class. In this case, the class cat inherits from the animal. We also define a constructor that pre-assigns the values to us as soon as a new instance of the class is created. We'll go into more detail on the constructors in the next article. Now you can also use this class:

" ld_legs = 0
DATA(lo_animal) = NEW lcl_animal( ).
DATA(ld_legs) = lo_animal->get_number_of_legs( ).

" ld_legs = 4
DATA(lo_cat) = NEW lcl_cat( ).
ld_legs = lo_cat->get_number_of_legs( ).

 

You can see from this example that both methods and the attributes are inherited by the class lcl_cat and can also be used there. With this you can also use the class lcl_animal for the implementation of other classes. If you later want to expand the class with additional attributes, then you only have to adapt the superclass lcl_animal and all inheriting classes will automatically receive the new properties and capabilities.

 

Conclusion

We hope this article has given you a deeper insight into general class and the subject of inheritance. In the following articles we will go into the depth of the development and also talk about the design of objects.


Included topics:
OOABAP OOClassInheritance
Comments (0)
Advertising

ABAP OO - Redefinition and Getter/Setter

Category - ABAP

In this article we look at the topic of redefinition and how getters and setters can help you with uniform interfaces.

06/18/2021

ABAP OO - Interface and abstract class

Category - ABAP

We had already introduced you to inheritance in ABAP OO and are now looking at interfaces and abstract classes and how they help you with modeling.

06/11/2021

ABAP OO - Dynamic, Static, Super

Category - ABAP

In this article we want to show you the difference between dynamic and static classes and what you can do with them. We also look at the various constructors.

06/04/2021

ABAP Unit Quick Guide

Category - ABAP

Our current e-book is now available on Amazon, we would like to briefly show you what you can expect in the book and what it will bring you in the end.

05/28/2021

ABAP OO - Basics

Category - ABAP

In this article we take a look at the basics of development in the object-oriented area and start a small series of articles.

04/30/2021