lecture 13 abap objects

Upload: nstomar

Post on 07-Jul-2018

255 views

Category:

Documents


2 download

TRANSCRIPT

  • 8/18/2019 Lecture 13 Abap Objects

    1/25

    Lecture 13

    ABAP Objects

    BCO5647 Applications Programming Techniques (ABAP)

  • 8/18/2019 Lecture 13 Abap Objects

    2/25

    2BCO5647

    Readings & Objectives

    Readings

    Keller & KellerChapter 5

    Section 5.3

    Objecties

    !his lect"re #ill

     Reie# the $roced"ral $rogra%%ing odel

     'ntrod"ce the Object Oriented $rogra%%ing odel

     ()a%ine ho# *B*$ has applied OO +eat"res to its lang"age

     ()a%ine ho# classes are de+ined in *B*$ Objects

     ()a%ine ho# attrib"tes and %ethods o+ a class are de+ined in *B*$ Objects

     ()a%ine the objects are created in *B*$ Objects

     ()a%ine ho# re+erences are assigned in *B*$ Objects

     ()plain the %eaning o+ a constr"ctor and ho# it is "sed in *B*$ Objects

     ,isc"ss the concept o+ inheritances and ho# it is "sed in *B*$ Objects

  • 8/18/2019 Lecture 13 Abap Objects

    3/25

    3BCO5647

    The Procedural Programming Model

    • Separation of data and functions.• Non-encapsulated (i.e. direct) access to data.

    • Possibility of encapsulating functions  using modularization.

    Data   Data

    Data Data

    Data

    Function

    FunctionFunction Function

    FunctionFunction FunctionFunction

  • 8/18/2019 Lecture 13 Abap Objects

    4/25

    4BCO5647

    The Procedural Programming Model

    •  Type denitions.

    •  Data declarations

    •  ain program- !alling subroutines- !alling function modules

    •  Denition of subroutines.

    report . . .*--------------------------------types: . . .

    data: . . .. . .perform form1 . . .call function ‘FB1’.. . .call function ‘FB2’.. . .

    *--------------------------------form f1 . . .  . . .endform.

  • 8/18/2019 Lecture 13 Abap Objects

    5/25

    5BCO5647

    The Object Oriented Programming Model

    "ncapsulation of Data and #unctions 

    !ree

    /o"se

    Crane

    Objects are an abstraction o+ the real #orld

    Objects are "nits %ade "p o+ data and o+ the

    +"nctions belonging to that data

    Real #orld&odel

    ,ata&ethod

     &ethod

    &ethod

    ,ata&ethod

     &ethod

    &ethod

    ,ata&ethod &ethod

    &ethod

    Boat

    ,ata&ethod

     &ethod

    &ethod

  • 8/18/2019 Lecture 13 Abap Objects

    6/25

    6BCO5647

    The Object Oriented Programming Model

    !lass

    • $i%es a general descriptionof ob&ects ('blueprint)

    • "stablises status types

    (attributes) and bea%ior(metods)

    *b&ect• +e,ection of real orld

    • Specic instance of a class

      lcl_class  Attribute

      Attribute

      Attribute  Method

      Method

  • 8/18/2019 Lecture 13 Abap Objects

    7/25

    7BCO5647

    The Object Oriented Programming Model

    • /P ob&ect statements can be used in procedural /Pprograms.

    • *b&ects (classes) contain procedural statements.

    report . . .

    *--------------------------------

    data: counter type i.  wa type kna1.

    . . .

    class lcl_car definition.

      . . .

    endclass.

    *------ main program ------

    counter = counter 1.

    create o!"ect . . .

    mo#e wa to . . .

  • 8/18/2019 Lecture 13 Abap Objects

    8/25

    0BCO5647

    The Object Oriented Programming Model

     Advantages of the Object-Oriented Programming Model

    over the Procedural Programming Model 

    •  0mpro%ed softare structure andconsistency in te de%elopment process.

    •  +educed maintenance e1ort and less susceptibility to errors.

    •  /etter integration of te customer2user into te analysis3 design3 and

    maintenance process.

    •  "asier and safer possibilities for e4tending te softare.

  • 8/18/2019 Lecture 13 Abap Objects

    9/25

    1BCO5647

    Uniied Modeling Language !UML"

    • 56 is a orldide standardized modelinglanguage.

    • 0t is used for te specication3 construction3%isualization and documentation of models forsoftare systems.

    • 56 contains a number of di1erent diagram typesin order to represent di1erent %ies of a system. Tese include 7

    •  !lass diagrams

    •  *b&ect diagrams

    •  5se !ase diagrams

    •  State Diagrams

    •  Se8uence diagrams etc.

  • 8/18/2019 Lecture 13 Abap Objects

    10/25

    BCO5647

    UML Re#resentation o a $lass

    • class is represented by a rectangle in 56 notation. #irst3te class9s name is gi%en3 ten its attributes3 and nally its

    metods.• ttributes describe te data tat can be stored in teob&ects of a class. Tey also determine te status of anob&ect.

    • etods describe te functions tat an ob&ect can perform. Tey terefore determine te ob&ect9s bea%ior.

  • 8/18/2019 Lecture 13 Abap Objects

    11/25

    BCO5647

    UML $lass %iagram

    class diagram describes all static relationsips beteen teclasses. Tere are to basic forms of static relationsips7

    Association0n tis e4ample7 customer boo:s a car at a rentalcar company.

    Generalization / Specialization0n tis e4ample7 car3 a bus3 and a truc: are all %eicles.

  • 8/18/2019 Lecture 13 Abap Objects

    12/25

    2BCO5647

    OO %einitions Objects

     

    $riate access

    • Encapsulation

    • As a rule, attributes

    $"blic access

    • Interface

    • As a rule,methods, events

    *ttrib"tes

    (ents

    *ttrib"tes

    &ethods

    (ents

    6a%e 8/ Berlin

    8ength 7 %

    -eight 39 :g

    landed

    &ethods

    +l;land

    ()a%ple airplane

    • Te ob&ect in te abo%e model as to layers7 an outer sell and aninner core. 5sers can only see te outer sell3 ile te inner coreremains idden.

    • Public components (outer sell)7 te outer sell contains tecomponents of te ob&ect tat are %isible to users.

    • Pri%ate components (inner core)7 te components of te inner core(attributes3 metods and e%ents) are only %isible itin te ob&ectitself.

  • 8/18/2019 Lecture 13 Abap Objects

    13/25

    3BCO5647

    OO %einitions $lasses

     

    $lane tic:et$lane

    • 0n te real orld3 tere are ob&ects3 suc as %arious airplanes and planetic:ets. Some of tese ob&ects are %ery similar3 tat is3 tey can bedescribed using te same attributes or caracteristics and pro%ide tesame functions.

    • Similar ob&ects are grouped togeter in classes. class is terefore adescription of a 8uantity of ob&ects caracterized by te same structureand te same bea%ior.

  • 8/18/2019 Lecture 13 Abap Objects

    14/25

    4BCO5647

    $lass %einition '(nta)

    $%&'' ()F+,+.

     . . .

    )+($%&''.

    $%&'' /%))+,&,+.

     . . .

    )+($%&''.

     

    • Denition Part Te class components (e.g. attributes and metods).

    • 0mplementation Part Te metod implementations.

    •  Te !6SS statement cannot be nested.

  • 8/18/2019 Lecture 13 Abap Objects

    15/25

    5BCO5647

    $lass %einition *)am#le

    0)/0, )&/%)1 .

    * $lass (efinitions.

    $%&'' lcl_airplane ()F+,+.  /3B%$ ')$,+.

      (&,&: name type strin45

      wei46t type i5

      carrier type strin4.

      ),7(': add_airplane5

      display_airplane5

    )+($%&''.*

    * $lass mplementations.*

    $%&'' lcl_airplane /%))+,&,+.

      ),7( add_airplane.

      . . .

      )+(),7(.

      ),7( display_airplane.

    . . .

      )+(),7(.

    )+($%&''.

  • 8/18/2019 Lecture 13 Abap Objects

    16/25

    6BCO5647

    OO %einitions Attributes

    • ttributes are te data ob&ects itin a class. Tey re,ect an

    ob&ects state.• ll /P data types can be used for attributes. Te DT

    statement is used to declare instance attributes.

    •  To declare a static attribute3 use te !6SS-DT statement.

    $%&''   ()F+,+.

    /08&,) ')$,+.

    . . .  types: . . .

      constants: . . .

      data: #aria!le1 type local-type5

      #aria!le2 type global_type5

      #aria!le9 like #aria!le15

      #aria!le type . . . read-only.

      #aria!le; type ref to class-name5

      #aria!le< type ref to type-name.

  • 8/18/2019 Lecture 13 Abap Objects

    17/25

    7BCO5647

    OO %einitions Methods

    • Methods are internal procedures in classes tat determinete bea%ior of te ob&ects.

    • etods a%e a signature (interface parameters ande4ceptions) tat enables tem to recei%e %alues en teyare called and pass %alues bac: to te calling program.

    • etods can a%e any number of 0P*+T0N$3 ";P*+T0N$3and !

  • 8/18/2019 Lecture 13 Abap Objects

    18/25

    0BCO5647

    $lass Methods '(nta)

    $%&'' ()F+,+.

     . . .

      ),7(':  

      /0,+ . . .  )/0,+ . . .

      $7&++ . . .

      0),30++ . . .  )$)/,+' . . .

    )+($%&''.

    $%&'' /%))+,&,+.

      ),7(  

      . . .

      )+(),7(

    )+($%&''.

     

  • 8/18/2019 Lecture 13 Abap Objects

    19/25

    1BCO5647

    $lass Methods *)am#le

    0)/0, )&/%)2.

    $%&'' lcl_airplane ()F+,+.

     /3B%$ ')$,+.

      (&,&: name type strin45

      wei46t type i5

      carrier type strin4.

      ),7(': init_airplane

      importin4 iname type strin4  iwei46t type i5

      display_airplane.)+($%&''.

    $%&'' lcl_airplane /%))+,&,+.

      ),7( init_airplane.

      name = iname.

      wei46t = iwei46t.

      carrier = ‘%7’.

      )+(),7(.

      ),7( display_airplane.  write:> ‘name :?5 name.

      )+(),7(.

    )+($%&''.

  • 8/18/2019 Lecture 13 Abap Objects

    20/25

    2BCO5647

    $reating Objects

    •*b&ects are created using te statement  !+"T" */="!T ref_name  . . .

    •  Tey can only be created and addressed using reference%ariables.

    • class contains te generic description of an ob&ect.

    • During te program runtime3 te class is used to create discreteob&ects (instances) in te memory. (instantiation).

    • 0f tis is te rst time te class is accessed3 te class is alsoloaded into te memory.

     

    lcl

  • 8/18/2019 Lecture 13 Abap Objects

    21/25

    2BCO5647

    $reating Objects *)am#le

    0)/0, )&/%)9.

    $%&'' lcl_airplane ()F+,+.

     /3B%$ ')$,+.

      (&,&: . . .

      ),7(': . . .

    )+($%&''.

    $%&'' lcl_airplane /%))+,&,+.

      ),7( init_airplane.  . . .

      )+(),7(.

      ),7( display_airplane.  . . .  )+(),7(.

    )+($%&''.

    ',&0,-F-')%)$,+.

    (&,& : airplane1 ,@/) 0)F to lcl_airplane5

      airplane2 ,@/) 0)F to lcl_airplane.

    $0)&,) BA)$, airplane1.$0)&,) BA)$, airplane2.

    $&%% ),7( airplane1-init_airplane

      eCportin4 iname = ‘($’

      iwei46t = 9;.

    $&%% ),7( airplane1-display_airplane.

  • 8/18/2019 Lecture 13 Abap Objects

    22/25

    22BCO5647

    Assigning Reerences

    0n te pre%ious e4ample3 te !+"T" */="!T statement createsan ob&ect in te main memory.

    0f te folloing statement is added after te ob&ects a%e beencreated 7

    airplane> ? airplane@

     Te reference %ariables ill be assigned to eac oter. *nce itas been assigned3 airplane> points to te same ob&ect as

    reference airplane@.

  • 8/18/2019 Lecture 13 Abap Objects

    23/25

    23BCO5647

    $onstructors

    CREATE

    OBJECT

    • constructor is a special metod for creating ob&ects it adened initial state.

    • 0t only as importing parameters and e4ceptions.

    • "4actly one constructor is dened per class.

    • 0t is e4ecuted e4actly once per instance.

    lcl_airplane

    name

    weight

    count

    constructor 

  • 8/18/2019 Lecture 13 Abap Objects

    24/25

    24BCO5647

    $onstructors *)am#le

    0)/0, )&/%).

    $%&'' lcl_airplane ()F+,+.

     /3B%$ ')$,+.  (&,&: name type strin45

      wei46t type i5  carrier type strin4.

      ),7(': constructor importin4

      icarrier type strin4.)+($%&''.

    $%&'' lcl_airplane /%))+,&,+.  ),7( constructor.

      $arrier = icarrier.  )+(),7(.

    )+($%&''.

    ',&0,-F-')%)$,+.(&,& : airplane1 ,@/) 0)F to lcl_airplane5

      airplane2 ,@/) 0)F to lcl_airplane.

    $0)&,) BA)$, airplane1 eCportin4

    icarrier = ‘%7’.$0)&,) BA)$, airplane2 eCportin4

      icarrier = ‘D&’.

  • 8/18/2019 Lecture 13 Abap Objects

    25/25

    25BCO5647

    +nheritance

    • 0neritance is a relationsip in ic one class (te subclass)inerits all te main caracteristics of anoter class (tesuperclass).

    • 0neritance is an implementation relationsip tat empasizessimilarities beteen classes.

    •  Te ineritance relationsip is often described as an 'is-arelationsip7 a passenger plane is an airplane.