25
Lecture 13 ABAP Objects BCO5647 Applications Programming Techniques (ABAP)

Lecture13 abap on line

  • Upload
    mkpatil

  • View
    1.407

  • Download
    2

Embed Size (px)

Citation preview

Lecture 13ABAP Objects

BCO5647 Applications Programming Techniques (ABAP)

2BCO5647

Readings & Objectives

Readings

Keller & Keller Chapter 5Section 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

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

4BCO5647

The Procedural Programming Model

• Type definitions.

• Data declarations

• Main program- Calling subroutines- Calling function modules

• Definition of subroutines.

report . . .*--------------------------------types: . . .data: . . .. . .perform form1 . . .call function ‘FB1’.. . .call function ‘FB2’.. . .

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

5BCO5647

The Object Oriented Programming Model

Encapsulation of Data and Functions SAP AG 1999

What Are Objects?

Tree

House

Crane

Objects are an abstraction of the real world

Objects are units made up of data and of thefunctions belonging to that data

Real worldModel

DataMethod MethodMethod

DataMethod MethodMethod

DataMethod MethodMethod

Boat

DataMethod MethodMethod

6BCO5647

The Object Oriented Programming Model

Class

• Gives a general description of objects (“blueprint”)

• Establishes status types (attributes) and behavior (methods)

Object

• Reflection of real world

• Specific instance of a class

lcl_class

Attribute

Attribute

Attribute Method

Method

7BCO5647

The Object Oriented Programming Model

• ABAP object statements can be used in procedural ABAP programs.

• Objects (classes) contain procedural statements.

report . . .*--------------------------------data: counter type i. wa type kna1.. . .

class lcl_car definition. . . .endclass.

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

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

8BCO5647

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.

9BCO5647

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

• Sequence diagrams etc.

10BCO5647

UML Representation of a Class

• A class is represented by a rectangle in UML notation. First, the class’s 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 object’s behavior.

11BCO5647

UML Class Diagram

A class diagram describes all static relationships between the classes.There are two basic forms of static relationships:

AssociationIn this example: A customer books a car at a rentalcar company.

Generalization / SpecializationIn this example: A car, a bus, and a truck are all vehicles.

12BCO5647

OO Definitions : Objects

S A P A G 1 9 9 9

T h e O b j e c t ( 1 )

P r i v a t e a c c e s s• E n c a p s u l a t i o n• A s a r u l e , a t t r i b u t e s

P u b l i c a c c e s s• I n t e r f a c e• A s a r u l e , m e t h o d s , e v e n t s

A t t r i b u t e s

E v e n t s

A t t r i b u t e s

M e t h o d s

E v e n t s

N a m e : L H B e r l i n

L e n g t h : 7 0 m

W e i g h t : 3 0 , 0 0 0 k g

l a n d e d

M e t h o d s

f l yl a n d

E x a m p l e : a i r p l a n e

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

13BCO5647

OO Definitions : Classes

S A P A G 1 9 9 9

C la s s if ic a tio n

P la n e tic k e tP la n e

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

14BCO5647

Class Definition : Syntax

CLASS <classname> DEFINITION.

. . .

ENDCLASS.

CLASS <classname> IMPLEMENTATION.

. . .

ENDCLASS.

• Definition PartThe class components (e.g. attributes and methods).

• Implementation PartThe method implementations.

• The CLASS statement cannot be nested.

15BCO5647

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.

16BCO5647

OO Definitions : Attributes

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

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.

17BCO5647

OO Definitions : Methods

• Methods are internal procedures in classes that determine the behavior of the objects.

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

SAP AG 1999

Methods

lcl_airplane

...

fly

land

Contain coding

Have an interfaceMethods :

• Contain coding

• Have an interface

18BCO5647

Class Methods : Syntax

CLASS <classname> DEFINITION.

. . .

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

ENDCLASS.

CLASS <classname> IMPLEMENTATION.

METHOD <methodname> . . . ENDMETHOD

ENDCLASS.

19BCO5647

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.

20BCO5647

Creating Objects

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

SAP AG 1999

Creating Objects

lcl_airplane

name

weight

... CREATE OBJECT

name: LH Berlinweight: 30,000 kg

Objects can only be created and addressed usingreference variables

21BCO5647

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.

22BCO5647

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.

23BCO5647

Constructors

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.

lcl_airplane

name

weight

countconstructor

24BCO5647

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

25BCO5647

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.