Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lecture13 abap on line

1,845 views

Published on

Published in: Technology
  • Be the first to comment

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>

×