Lecture13 abap on line

1,790 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,790
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
253
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Data and functions are usually kept separate in the procedural programming model. Generally, global variables for a program contain data, subroutines contain functions. Essentially, every subprogram can access every variable. This means that the programming model itself does not support consistent access to some related parts of the data.
  • A typical procedural ABAP program consists of type definitions and data declarations, which describe the structure of the data the program uses when it is executed. Modularization units, for example, subroutines or function modules can be encapsulated. However, on the main program level, there is no special protection for the data objects. Any variables can be accessed by any means.
  • Object orientation focuses on objects that represent either abstract or concrete things in the real world. They are first viewed in terms of their characteristics, which are mapped using the object’s internal structure and attributes (data). The behavior of an object is described through methods and events (functionality). Objects form capsules containing the data itself and the behavior of that data. Objects should enable you to draft a software solution that is a one-to-one mapping of the real-life problem area.
  • Object orientation focuses on objects that represent either abstract or concrete things in the real world. They are first viewed in terms of their characteristics, which are mapped using the object’s internal structure and attributes (data). The behavior of an object is described through methods and events (functionality). Objects form capsules containing the data itself and the behavior of that data. Objects should enable you to draft a software solution that is a one-to-one mapping of the real-life problem area.
  • ABAP Objects is not a new language, but has been designed as a systematic extension of ABAP. All of the extensions, including the old procedural parts, are upwardly compatible. Type checks in the object-oriented contexts of ABAP Objects are stricter than those in the procedural contexts. In developing ABAP Objects, the ABAP language was cleaned up, in particular in the object-oriented contexts. This means that obsolete statements lead to syntax errors. However, it is also advisable to avoid obsolete statements in the purely procedural environment, as this creates source texts that are safer and more flexible. Nevertheless, as the language is upwardly compatible, it is not possible to prevent the use of such statements entirely.
  • Consistency throughout the software development process The “language” used in the various phases of software development (analysis, specification, design and implementation) is uniform. The ideal would be for changes made during the implementation phase to flow back into the design automatically. Encapsulation Encapsulation means that the implementation of an object is hidden from other components in the system, so that they cannot make assumptions about the internal status of the object and therefore dependencies on specific implementations do not arise. Polymorphism Polymorphism (ability to have multiple forms) in the context of object technology signifies that objects in different classes have different reactions to the same message. Inheritance Inheritance defines the implementation relationship between classes, in which one class (the subclass) shares the structure and the behavior defined in one or more other classes (superclasses). Note: ABAP Objects only allows single inheritance.
  • UML (Unified Modeling Language) is a standardized modeling language. It is used for the specification, construction, visualization and documentation of models for software systems and enables uniform communication between various users. UML does not describe the steps in the object-oriented development process. UML is an industry standard and has been standardized by the OMG (Object Management Group) since September 1997 as UML Version 1.1. The members of the OMG are continuously developing it further. SAP uses UML as the company-wide standard for object-oriented modeling. You can find the UML specifications on the OMG homepage at: http://www.omg.org
  • A class is represented by a rectangle in UML notation. First, the class’s name is given, then its attributes, and finally its methods. However, you also have the option of omitting the attribute part and/or the method part. Attributes describe the data that can be stored in the objects of a class. They also determine the status of an object. Methods describe the functions that an object can perform. They therefore determine the object’s behavior.
  • A class diagram describes all static relationships between the classes. There are two basic forms of static relationships: Association In this example: A customer books a car at a rental car company. Generalization / Specialization In this example: A car, a bus, and a truck are all vehicles. Note: As mentioned previously, classes can also be shown in class diagrams with their attributes and methods. Here, these have been left out to improve clarity.
  • The object in the above model has two layers: an outer shell and an inner core. Users can only see the outer shell, while the inner core remains hidden (the internal status of an object can only be seen within the object itself). Public components (outer shell): the outer shell contains the components of the object that are visible to users, such as attributes (data), methods (functions) and events. All users have direct access to these components. The public components of an object form its external point of contact. Private components (inner core): the components of the inner core (attributes, methods and events) are only visible within the object itself. The attributes of an object are generally private. These private attributes of an object can only be accessed using the methods of that object itself. Why are the private components of an object “hidden”? This principle is called “information hiding” or “encapsulation” and is used to protect the user. Let us assume that an object changes its private components, while its external point of contact remains unchanged. Any user who simply needs to access the object’s external point of contact can carry on working with the object as usual. The user does not notice the change. However, if an object changes its public components, then any user who accesses these public components must take these changes into account.
  • In the real world, there are objects, such as various airplanes and plane tickets. Some of these objects are very similar, that is, they can be described using the same attributes or characteristics and provide the same functions. Similar objects are grouped together in classes. Each class is described once, and each object is then created in accordance with this blueprint. A class is therefore a description of a quantity of objects characterized by the same structure and the same behavior. An object is a concrete example of a class, the airplane class is a description of the objects LH Munich, LH New York etc.. Objects that belong to the same class have the same attributes and can be accessed using the same methods. There is only one of each class within a software system, but each class can contain several objects.
  • A class is a description of a number of objects that have the same structure and the same behavior. A class is therefore like a blueprint, in accordance with which all objects in that class are created. The components of the class are defined in the definition part. The components are attributes, methods, events, constants, types and implemented interfaces. Only methods are implemented in the implementation part. The CLASS statement cannot be nested, that is, you cannot define a class within a class.
  • Note the sections within the Class definition . ABAP Objects supports three visibility sections in a class : PUBLIC : All components in this section are public and can be addressed by all users and methods. They form the external interface of the class. PROTECTED : The components of this section are protected and can be addressed by the methods of subclasses (inheritance) and methods of the class itself. PRIVATE : Components of this section are private and can only be used in the methods of the class itself.
  • In classes, you can only use the TYPE addition to refer to data types . You can only use the LIKE reference for local data objects . The READ-ONLY addition means that a public attribute that was declared with DATA can be read from outside, but can only be changed by methods in the same class. You can currently only use the READ-ONLY addition in the public visibility section (PUBLIC SECTION) of a class declaration or in an interface definition. Using TYPE REF TO, an attribute can be typed as any reference.
  • Methods are internal procedures in classes that determine the behavior of the objects. They can access all attributes in their class and can therefore change the state of other elements. Methods have a signature (interface parameters and exceptions) that enables them to receive values when they are called and pass values back to the calling program. Methods can have any number of IMPORTING, EXPORTING, and CHANGING parameters. All parameters can be passed by value or reference.
  • In ABAP Objects, methods can have IMPORTING, EXPORTING, CHANGING and RETURNING parameters as well as EXCEPTIONS. All parameters can be passed by value or reference. You can define a return code for methods using RETURNING. You can only do this for a single parameter, which additionally must be passed as a value. Also, you cannot then define EXPORTING and CHANGING parameters. You can define functional methods using the RETURNING parameter (explained in more detail below). All input parameters (IMPORTING, CHANGING parameters) can be defined as optional parameters in the declaration using the OPTIONAL or DEFAULT additions. These parameters then do not necessarily have to be passed when the object is called. If you use the OPTIONAL addition, the parameter remains initialized according to type, whereas the DEFAULT addition allows you to enter a start value.
  • A class contains the generic description of an object. It describes all the characteristics that are common to all the objects in that class. During the program runtime, the class is used to create specific objects (instances). This process is called instantiation. Example: The object LH Berlin is created during runtime in the main memory by instantiation from the lcl_airplane class. The lcl_airplane class itself does not exist as an independent runtime object in ABAP Objects. Realization: Objects are instantiated using the statement: CREATE OBJECT. During instantiation, the runtime environment dynamically requests main memory space and assigns it to the object.
  • Reference variables can also be assigned to each other. The above example shows that once it has been assigned, airplane1 points to the same object as reference airplane2. As soon as no more references point to an object, the Garbage Collector removes it from the memory. The Garbage Collector is a system routine that automatically deletes objects that can no longer be addressed from the main memory and releases the memory space they occupied.
  • The constructor is a special (instance) method in a class and is always named CONSTRUCTOR. The following rules apply: Each class has exactly one constructor. The constructor does not need to be defined if no implementation is defined. The constructor is automatically called during runtime within the CREATE OBJECT statement. If you need to implement the constructor, then you must define and implement it in the PUBLIC SECTION. When EXCEPTIONS are triggered in the constructor, instances are not created (as of 4.6a), so no main memory space is taken up. You need to implement the constructor when, for example You need to allocate (external) resources You need to initialize attributes that cannot be covered by the VALUE supplement to the DATA statement You need to modify static attributes You cannot normally call the constructor explicitly.
  • Inheritance is a relationship in which one class (the subclass) inherits all the main characteristics of another class (the superclass). The subclass can also add new components (attributes, methods, and so on) and replace inherited methods with its own implementations. Inheritance is an implementation relationship that emphasizes similarities between classes. In the example above, the similarities between the passenger plane and cargo plane classes are extracted to the airplane superclass. This means that common components are only defined/implemented in the superclass and are automatically present in the subclasses. The inheritance relationship is often described as an “is-a” relationship: a passenger plane is an airplane.
  • Lecture13 abap on line

    1. 1. Lecture 13 ABAP Objects BCO5647 Applications Programming Techniques (ABAP)
    2. 2. Readings & Objectives <ul><li>Readings </li></ul><ul><li>Keller & Keller Chapter 5 Section 5.3 </li></ul><ul><li>Objectives This lecture will </li></ul><ul><li>Review the Procedural Programming Model </li></ul><ul><li>Introduce the Object Oriented Programming Model </li></ul><ul><li>Examine how ABAP has applied OO features to its language </li></ul><ul><li>Examine how classes are defined in ABAP Objects </li></ul><ul><li>Examine how attributes and methods of a class are defined in ABAP Objects </li></ul><ul><li>Examine the objects are created in ABAP Objects </li></ul><ul><li>Examine how references are assigned in ABAP Objects </li></ul><ul><li>Explain the meaning of a constructor and how it is used in ABAP Objects </li></ul><ul><li>Discuss the concept of inheritances and how it is used in ABAP Objects </li></ul>
    3. 3. The Procedural Programming Model <ul><li>Separation of data and functions. </li></ul><ul><li>Non-encapsulated (i.e. direct) access to data. </li></ul><ul><li>Possibility of encapsulating functions using modularization. </li></ul>Data Data Data Data Data Function Function Function Function Function Function Function Function
    4. 4. The Procedural Programming Model <ul><li>Type definitions. </li></ul><ul><li>Data declarations </li></ul><ul><li>Main program - Calling subroutines - Calling function modules </li></ul><ul><li>Definition of subroutines. </li></ul>report . . . *-------------------------------- types: . . . data: . . . . . . perform form1 . . . call function ‘FB1’. . . . call function ‘FB2’. . . . *-------------------------------- form f1 . . . . . . endform.
    5. 5. The Object Oriented Programming Model Encapsulation of Data and Functions
    6. 6. The Object Oriented Programming Model <ul><li>Class </li></ul><ul><li>Gives a general description of objects (“blueprint”) </li></ul><ul><li>Establishes status types (attributes) and behavior (methods) </li></ul><ul><li>Object </li></ul><ul><li>Reflection of real world </li></ul><ul><li>Specific instance of a class </li></ul>lcl_class Attribute Attribute Attribute Method Method
    7. 7. The Object Oriented Programming Model <ul><li>ABAP object statements can be used in procedural ABAP programs. </li></ul><ul><li>Objects (classes) contain procedural statements. </li></ul>report . . . *-------------------------------- data: counter type i. wa type kna1. . . . class lcl_car definition. . . . endclass. *------ main program ------ counter = counter + 1. create object . . . move wa to . . .
    8. 8. The Object Oriented Programming Model <ul><li>Advantages of the Object-Oriented Programming Model over the Procedural Programming Model </li></ul><ul><li>Improved software structure and consistency in the development process. </li></ul><ul><li>Reduced maintenance effort and less susceptibility to errors. </li></ul><ul><li>Better integration of the customer/user into the analysis, design, and maintenance process. </li></ul><ul><li>Easier and safer possibilities for extending the software. </li></ul>
    9. 9. Unified Modeling Language (UML) <ul><li>UML is a worldwide standardized modeling language. </li></ul><ul><li>It is used for the specification, construction, visualization and documentation of models for software systems. </li></ul><ul><li>UML contains a number of different diagram types in order to represent different views of a system. These include : </li></ul><ul><ul><ul><li>Class diagrams </li></ul></ul></ul><ul><ul><ul><li>Object diagrams </li></ul></ul></ul><ul><ul><ul><li>Use Case diagrams </li></ul></ul></ul><ul><ul><ul><li>State Diagrams </li></ul></ul></ul><ul><ul><ul><li>Sequence diagrams etc. </li></ul></ul></ul>
    10. 10. UML Representation of a Class <ul><li>A class is represented by a rectangle in UML notation. First, the class’s name is given, then its attributes, and finally its methods. </li></ul><ul><li>Attributes describe the data that can be stored in the objects of a class. They also determine the status of an object. </li></ul><ul><li>Methods describe the functions that an object can perform. They therefore determine the object’s behavior. </li></ul>
    11. 11. UML Class Diagram A class diagram describes all static relationships between the classes. There are two basic forms of static relationships: Association In this example: A customer books a car at a rental car company. Generalization / Specialization In this example: A car, a bus, and a truck are all vehicles.
    12. 12. OO Definitions : Objects <ul><li>The object in the above model has two layers: an outer shell and an inner core. Users can only see the outer shell, while the inner core remains hidden. </li></ul><ul><li>Public components (outer shell): the outer shell contains the components of the object that are visible to users. </li></ul><ul><li>Private components (inner core): the components of the inner core (attributes, methods and events) are only visible within the object itself. </li></ul>
    13. 13. OO Definitions : Classes <ul><li>In the real world, there are objects, such as various airplanes and plane tickets. Some of these objects are very similar, that is, they can be described using the same attributes or characteristics and provide the same functions. </li></ul><ul><li>Similar objects are grouped together in classes. A class is therefore a description of a quantity of objects characterized by the same structure and the same behavior. </li></ul>
    14. 14. Class Definition : Syntax CLASS <classname> DEFINITION. . . . ENDCLASS. CLASS <classname> IMPLEMENTATION. . . . ENDCLASS. <ul><li>Definition Part The class components (e.g. attributes and methods). </li></ul><ul><li>Implementation Part The method implementations. </li></ul><ul><li>The CLASS statement cannot be nested. </li></ul>
    15. 15. Class Definition : Example REPORT EXAMPLE01 . * Class Definitions. CLASS lcl_airplane DEFINITION. PUBLIC SECTION. DATA: name type string, weight type i, carrier type string. METHODS: add_airplane, display_airplane, ENDCLASS. * * Class Implementations. * CLASS lcl_airplane IMPLEMENTATION. METHOD add_airplane. . . . ENDMETHOD. METHOD display_airplane. . . . ENDMETHOD. ENDCLASS.
    16. 16. OO Definitions : Attributes <ul><li>Attributes are the data objects within a class. They reflect an objects state. </li></ul><ul><li>All ABAP data types can be used for attributes. The DATA statement is used to declare instance attributes. </li></ul><ul><li>To declare a static attribute, use the CLASS-DATA statement. </li></ul>CLASS <classname> DEFINITION. PRIVATE SECTION. . . . types: . . . constants: . . . data: variable1 type local-type , variable2 type global_type , variable3 like variable1, variable4 type . . . read-only. variable5 type ref to class-name , variable6 type ref to type-name.
    17. 17. OO Definitions : Methods <ul><li>Methods are internal procedures in classes that determine the behavior of the objects. </li></ul><ul><li>Methods have a signature (interface parameters and exceptions) that enables them to receive values when they are called and pass values back to the calling program. </li></ul><ul><li>Methods can have any number of IMPORTING, EXPORTING, and CHANGING parameters. All parameters can be passed by value or reference. </li></ul><ul><li>Methods : </li></ul><ul><li>Contain coding </li></ul><ul><li>Have an interface </li></ul>
    18. 18. Class Methods : Syntax CLASS <classname> DEFINITION. . . . METHODS: <methodname> IMPORTING . . . EXPORTING . . . CHANGING . . . RETURNING . . . EXCEPTIONS . . . ENDCLASS. CLASS <classname> IMPLEMENTATION. METHOD <methodname> . . . ENDMETHOD ENDCLASS.
    19. 19. Class Methods : Example REPORT EXAMPLE02. CLASS lcl_airplane DEFINITION. PUBLIC SECTION. DATA: name type string, weight type i, carrier type string. METHODS: init_airplane importing iname type string iweight type i, display_airplane. ENDCLASS. CLASS lcl_airplane IMPLEMENTATION. METHOD init_airplane. name = iname. weight = iweight. carrier = ‘LH’. ENDMETHOD. METHOD display_airplane. write:/ ‘name :', name. ENDMETHOD. ENDCLASS.
    20. 20. Creating Objects <ul><li>Objects are created using the statement CREATE OBJECT ref_name . . . </li></ul><ul><li>They can only be created and addressed using reference variables. </li></ul><ul><li>A class contains the generic description of an object. </li></ul><ul><li>During the program runtime, the class is used to create discrete objects (instances) in the memory. ( instantiation) . </li></ul><ul><li>If this is the first time the class is accessed, the class is also loaded into the memory. </li></ul>
    21. 21. Creating Objects : Example REPORT EXAMPLE03. CLASS lcl_airplane DEFINITION. PUBLIC SECTION. DATA: . . . METHODS: . . . ENDCLASS. CLASS lcl_airplane IMPLEMENTATION. METHOD init_airplane. . . . ENDMETHOD. METHOD display_airplane. . . . ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA : airplane1 TYPE REF to lcl_airplane, airplane2 TYPE REF to lcl_airplane. CREATE OBJECT airplane1. CREATE OBJECT airplane2. CALL METHOD airplane1->init_airplane exporting iname = ‘DC40’ iweight = 3500. CALL METHOD airplane1->display_airplane.
    22. 22. Assigning References In the previous example, the CREATE OBJECT statement creates an object in the main memory. If the following statement is added after the objects have been created : airplane1 = airplane2 The reference variables will be assigned to each other. Once it has been assigned, airplane1 points to the same object as reference airplane2.
    23. 23. Constructors CREATE OBJECT <ul><li>A constructor is a special method for creating objects with a defined initial state. </li></ul><ul><li>It only has importing parameters and exceptions. </li></ul><ul><li>Exactly one constructor is defined per class. </li></ul><ul><li>It is executed exactly once per instance. </li></ul>lcl_airplane name weight count constructor
    24. 24. Constructors : Example REPORT EXAMPLE04. CLASS lcl_airplane DEFINITION. PUBLIC SECTION. DATA: name type string, weight type i, carrier type string. METHODS: constructor importing icarrier type string. ENDCLASS. CLASS lcl_airplane IMPLEMENTATION. METHOD constructor. Carrier = icarrier. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA : airplane1 TYPE REF to lcl_airplane, airplane2 TYPE REF to lcl_airplane. CREATE OBJECT airplane1 exporting icarrier = ‘LH’. CREATE OBJECT airplane2 exporting icarrier = ‘QA’.
    25. 25. Inheritance <ul><li>Inheritance is a relationship in which one class (the subclass) inherits all the main characteristics of another class (the superclass). </li></ul><ul><li>Inheritance is an implementation relationship that emphasizes similarities between classes. </li></ul><ul><li>The inheritance relationship is often described as an “is-a” relationship: a passenger plane is an airplane. </li></ul>

    ×