Sie sind auf Seite 1von 25

Lecture 13

ABAP Objects

BCO5647 Applications Programming Techniques (ABAP)

Readings & Objectives


Readings
Keller & Keller
Chapter 5
Section 5.3

Objectives
This lecture will
Review the Procedural Programming Model
Introduce the Object Oriented Programming Model
Examine how ABAP has applied OO features to its language
Examine how classes are defined in ABAP Objects
Examine how attributes and methods of a class are defined in ABAP Objects
Examine the objects are created in ABAP Objects
Examine how references are assigned in ABAP Objects
Explain the meaning of a constructor and how it is used in ABAP Objects
Discuss the concept of inheritances and how it is used in ABAP Objects

BCO5647

The Procedural Programming Model

Data

Data
Data

Data
Data

Function
Function
Function

Function
Function

Function
Function

Function

Separation of data and functions.


Non-encapsulated (i.e. direct) access to data.
Possibility of encapsulating functions
using modularization.
BCO5647

The Procedural Programming Model


report . . .
*-------------------------------types: . . .
data: . . .
. . .
perform form1 . . .
call function FB1.
. . .
call function FB2.
. . .
*-------------------------------form f1 . . .
. . .
endform.

Type definitions.

Data declarations
Main program
- Calling subroutines
- Calling function modules
Definition of subroutines.
BCO5647

The Object Oriented Programming Model


What Are Objects?

Real world

Model

House

Tree

Data

Method
Method
Method

Data

Method
Method
Method

Crane

Data

Method
Method
Method

Boat

Data

Method
Method
Method

Objects are an abstraction of the real world


Objects are units made up of data and of the
functions belonging to that data

SAP AG 1999

Encapsulation of Data and Functions

BCO5647

The Object Oriented Programming Model

lcl_class

Class
Gives a general description
of objects (blueprint)
Establishes status types
(attributes) and behavior
(methods)

Attribute
Attribute
Attribute
Method
Method

Object
Reflection of real world
Specific instance of a class
BCO5647

The Object Oriented Programming Model


report . . .
*-------------------------------data: counter type i.
wa type kna1.
. . .
class lcl_car definition.
. . .
endclass.
*------

main program ------

counter = counter + 1.
create object . . .
move wa to . . .

ABAP object statements can be used in procedural ABAP


programs.
Objects (classes) contain procedural statements.
BCO5647

The Object Oriented Programming Model


Advantages of the Object-Oriented Programming Model
over the Procedural Programming Model

Improved software structure and


consistency in the development process.

Reduced maintenance effort and less


susceptibility to errors.

Better integration of the customer/user


into the analysis, design, and
maintenance process.

Easier and safer possibilities for


extending the software.

BCO5647

Unified Modeling Language (UML)


UML is a worldwide standardized modeling
language.
It is used for the specification, construction,
visualization and documentation of models for
software systems.
UML contains a number of different diagram types
in order to represent different views of a system.
These include :
Class diagrams
Object diagrams
Use Case diagrams
State Diagrams
BCO5647

9
Sequence diagrams

etc.

UML Representation of a Class

A class is represented by a rectangle in UML notation. First,


the classs name is given, then its attributes, and finally its
methods.
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 objects behavior.
BCO5647

10

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.
BCO5647

11

OO Definitions : Objects
The Object (1)
Example: airplane

Attributes

Attributes
Name: LH Berlin

Methods

Weight: 30,000 kg

land

Length: 70 m

fly

Methods
Events

Events
landed

Private access
Encapsulation
As a rule, attributes

Public access
Interface
As a rule,methods, events

SAP AG 1999

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.
Public components (outer shell): the outer shell contains the
components of the object that are visible to users.
Private components (inner core): the components of the inner core
(attributes, methods and events) are only visible within the object
itself.
BCO5647

12

OO Definitions : Classes
Classification

Plane

Plane ticket

SAP AG 1999

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. A class is therefore a
description of a quantity of objects characterized by the same structure
and the same behavior.

BCO5647

13

Class Definition : Syntax


CLASS <classname> DEFINITION.
. . .
ENDCLASS.

CLASS <classname> IMPLEMENTATION.


. . .
ENDCLASS.

Definition Part
The class components (e.g. attributes and methods).
Implementation Part
The method implementations.
The CLASS statement cannot be nested.
BCO5647

14

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.

BCO5647

15

OO Definitions : Attributes
CLASS <classname> DEFINITION.
PRIVATE SECTION.
. . .
types: . . .
constants: . . .
data: variable1
variable2
variable3

type local-type,
type global_type,
like variable1,

variable4

type . . . read-only.

variable5
variable6

type ref to class-name,


type ref to type-name.

Attributes are the data objects within a class. They reflect an


objects state.
All ABAP data types can be used for attributes.
The DATA
statement is used to declare instance attributes.
To declare a static attribute, use the CLASS-DATA statement.
BCO5647

16

OO Definitions : Methods

Contain coding

Have an interface

lcl_airplane

Methods :

...

Contain coding

fly

Have an interface

land

Methods are internal procedures in classes that determine


the behavior of the objects.

SAP AG 1999

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.

BCO5647

17

Class Methods : Syntax


CLASS <classname> DEFINITION.
. . .
METHODS:

<methodname>
IMPORTING . . .
EXPORTING . . .
CHANGING . . .
RETURNING . . .
EXCEPTIONS . . .

ENDCLASS.

CLASS <classname> IMPLEMENTATION.


METHOD <methodname>
. . .
ENDMETHOD
ENDCLASS.

BCO5647

18

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.

BCO5647

19

Creating Objects
Objects can only be created and addressed using
reference variables

lcl_airplane
name
weight
...

CREATE OBJECT

name:
LH Berlin
weight: 30,000 kg

SAP AG 1999

Objects are created using the statement


CREATE OBJECT ref_name . . .
They can only be created and addressed using reference
variables.
A class contains the generic description of an object.
During the program runtime, the class is used to create discrete
objects (instances) in the memory. (instantiation).
If this is the first time the class is accessed, the class is also
loaded into the memory.
BCO5647

20

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.

BCO5647

21

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.

BCO5647

22

Constructors

lcl_airplane
name
weight
count
constructor

CREATE
OBJECT

A constructor is a special method for creating objects with a


defined initial state.
It only has importing parameters and exceptions.
Exactly one constructor is defined per class.
It is executed exactly once per instance.
BCO5647

23

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.

BCO5647

24

Inheritance

Inheritance is a relationship in which one class (the subclass)


inherits all the main characteristics of another class (the
superclass).
Inheritance is an implementation relationship that emphasizes
similarities between classes.
The inheritance relationship is often described as an is-a
relationship: a passenger plane is an airplane.
BCO5647

25

Das könnte Ihnen auch gefallen