You are on page 1of 19

ABAP Objects

Topic Objectives

Describe the Advantages of Object Orientation Define Classes, Attributes, and Methods Define the Reference Concept Describe Interfaces and Events

Advantages of Object Orientation

Support of GUI and Workflow

Integration of External Object Models


Distribution Generic Programming and Reusability

Encapsulation

Interface Creation

Encapsulation

Program: . . .

Object: Flight Method: book

add 1 to passenger_list

Data: passenger_list

No access

Program: . . . Read table passenger_list

Function Modules vs. Objects


Function Group/ Module Objects

Function Group1 with data

Function Group N with data

Objects of Class 1 with data

Objects of Class N with data

ABAP Program with data

ABAP Program with data

Internal Session of ABAP Program GUI Session

Internal Session of ABAP Program GUI Session

Overview of Object-Oriented Concepts

A object is a self-contained entity having a state, a behavior, and an identity.

Object Attributes
CONNID 0400 19980126 FLDATE

PassengerList Miller Carter LH Smith

Private Access
CARRID Method: BOOK

Public Access

Objects Belonging to Classes


Class Flight Class Hotel

Instances

Instances

Classes
CLASS <class> DEFINITION. * INHERITING FROM <superclass> -- available in a later 4.0 version

PUBLIC SECTION. definition of components visible outside the class PROTECTED SECTION. definition of components only visible to subclasses of this class. PRIVATE SECTION. definition of components only visible within the class ENDCLASS. CLASS <class> IMPLEMENTATION. method implementations ENDCLASS.

Class Components: Attributes & Methods


{DATA

| CLASS-DATA} attr TYPE type [ [ [ [ VALUE val ] READ-ONLY ] VIRTUAL [ SET-METHOD set-method ] GET-METHOD get-method ] ].

CONSTANTS const TYPE type VALUE val.

{METHODS

[ [ [ [ [

| CLASS-METHODS} method IMPORTING <list of import parameters> ] EXPORTING <list of export parameters> ] CHANGING <list of import/export parameters> ] EXCEPTIONS <list of export exceptions> ] RETURNING result TYPE t ].

Reference Concept
Object Reference Object Reference

Instances

The Reference Concept Example


REPORT Flightdemo. TYPES: tpassenger(20). CLASS c_flight DEFINITION. PUBLIC SECTION. DATA: carrid like spfli-carrid, connid like spfli-connid, fldate like spfli-fldate. METHODS: book IMPORTING passenger TYPE tpassenger. PRIVATE SECTION. DATA: passengerlist type tpassenger occurs 10. ENDCLASS. CLASS c_flight IMPLEMENTATION. METHOD book. APPEND passenger to passengerlist. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA: flight TYPE REF to c_flight. CREATE OBJECT flight. Flight->carrid = LH. Flight->connid = 0567. Flight->fldate = 19970908. CALL METHOD flight->book EXPORTING passenger = Peter Miller. b

Interfaces
Interface Reservation

Class Flight Instances

Class Hotel
Instances

Generic Programming with Interfaces

INTERFACE interface. [ INTERFACES <list of comprised interfaces> ] [ <definition of interface components> ] ENDINTERFACE.

Example of Programming Interface

Events
Event Handling Classes Event raising class Event Table

Event Source

Event & Event Handlers Example


CLASS CGUIbutton DEFINITION. PUBLIC SECTION. METHODS: SetLabel IMPORTING txt TYPE . EVENTS: Clicked EXPORTING doubleclick TYPE I. ENDCLASS. CLASS window1 DEFINITION. PUBLIC SECTION. INTERFACES EVENTS OF CGUIbutton AS: OKBtn, CancelBtn. DATA: OK TYPE REF TO CGUIbutton. ENDCLASS. CLASS window1 IMPLEMENTATION. METHOD init. CREATE OBJECT OK. SET HANDLER OKBtn FOR EVENTS OF OK. ENDMETHOD. METHOD OKBtn~Clicked. If doubleclick = 1. Endif. ENDMETHOD. METHOD CancelBtn~Clicked. ENDMETHOD. ENDCLASS.

CLASS CGUIbutton IMPLEMENTATION. METHOD anymethod. RAISE EVENT Clicked EXPORTING doubleclick = 0. ENDMETHOD. ENDCLASS.

Exercise
Exercise 47 - Creating and Using a Class, 60 minutes

Questions and Answers

Any questions?

You might also like