Professional Documents
Culture Documents
INCTURE 2011 / #
Objectives
The participants will be able to:
Recognize the concept of Object Oriented Programming (OOP) Identify the features of Object Oriented Programming Recall the history of ABAP Object Oriented Programming Advantages of ABAP OOP over conventional ABAP Procedural Programming Analyze the basic building blocks of ABAP Objects
2
INCTURE 2011 / 2
3
INCTURE 2011 / 3
4
INCTURE 2011 / 4
Ecapsulation
Hiding data and its related logic behind well defined interfaces.
Inheritance
Polymorphism
Code Reuse
5
INCTURE 2011 / 5
6
INCTURE 2011 / 6
7
INCTURE 2011 / 7
8
INCTURE 2011 / 8
Classes and Objects are the basic building blocks of Object Oriented Programming. When a real world entity is modeled into OOP world then it is known as Class, characteristics as attributes and functionality as methods. Objects is an instance of a Class.
Example : What are the characteristics of the box? (Attributes) Inside color is blue (Private) Outside color is white (Public) What is the status of the box ? (Events) The box is semi open
INCTURE 2011 / 11
Functions of the box? (Methods) It can store things It can occupy space
11
Global Classes
Any Program
Local Classes
Only with in the Program where it is defined In the program where it is defined With ABAP editor (SE38) Any 12
Classes are template for Objects. This declares and defines a local class test . In ABAP program this belongs to Global Section. Class definition cannot be nested. Classes cannot be defined inside subroutines or function modules. A class definition declares :
Its components : Attributes, Methods, Events. The visibility of its components : Public, Protected and Private.
CLASS test IMPLEMENTATION. <class body> {Method implementation is done here} ENDCLASS.
13
INCTURE 2011 / 13
Instance components:
DATA
For instance attributes
METHODS
For instance methods
EVENTS
For instance events
Static components:
CLASS-DATA
For static attributes
CLASS-METHODS
For static methods
CLASS-EVENTS
For static events
CONSTANTS
For constants
Instance components exist separately in each instance (object) of the class. Static components only exist one per class and are valid for all instances of the class. Static components are declared with the CLASS- * keywords. To access instance components, instance component selector (->) is used. To access static components, static component selector (=>) is used.
14
INCTURE 2011 / 14
Private components can only be used in the methods of the class itself.
15
INCTURE 2011 / 15
Methods
CLASS c1 DEFINITION. PUBLIC SECTION. METHODS: do_something IMPORTING ...i1 TYPE EXPORTINGe1 TYPE CHANGING c1 TYPE EXCEPTIONS en. PRIVATE SECTION. DATA: ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD do_something. ENDMETHOD. ENDCLASS.
Methods are the functionality of a class , ABAP codes are written within a method to incorporate the functionality. Methods are processing blocks with a parameter interface very similar to function modules. Methods are of two types:
Standard Methods.
e.g. METHODS meth.
16
INCTURE 2011 / 16
Event Handling
Events are handled by classes
General publish-subscribe model Syntax similar to Visual Basic event handling
Event handlers...
...are methods for handling events from other objects ...are declared with reference to the event to be handled (signature from there) must be registered explicitly
17
INCTURE 2011 / 17
Handler
CLASS CWindow1 DEFINITION. PUBLIC SECTION. "--- handle events by implementing "--- event handler methods METHODS: OKClicked FOR EVENT Clicked OF CButton IMPORTING DoubleClick, CanClicked FOR EVENT Clicked OF CButton. DATA: OKBtn TYPE REF TO CButton. ENDCLASS. CLASS CWindow1 IMPLEMENTATION. METHOD Init. CREATE OBJECT: OKBtn, CanBtn. SET HANDLER: OKClicked FOR OKBtn, CanClicked FOR CanBtn. ENDMETHOD. METHOD OKClicked. IF DoubleClick = 1. ENDMETHOD.
CLASS CButton IMPLEMENTATION. METHOD AnyMethod. RAISE EVENT Clicked EXPORTING DoubleClick = 0. ENDMETHOD. ENDCLASS.
ENDIF.
18
INCTURE 2011 / 18
Constructors
METHODS constructor
IMPORTING
EXPORTING CREATE OBJECT obj EXPORTING Instance constructor
Each class has one constructor. It is a predefined, public instance method of the class, with the name CONSTRUCTOR (or CLASS_CONSTRUCTOR for static constructor). Constructors are special methods that produce a defined initial state of objects and classes. Constructors are executed once for each instance. They are called automatically after you have created an instance of the class with the CREATE OBJECT statement.
19
INCTURE 2011 / 19
CLASS class_name DEFINITION CREATE PUBLIC| PROTECTED | PRIVATE. CREATE PUBLIC addition is provided automatically by compiler if no create addition is used. The additions CREATE PROTECTED and CREATE PRIVATE allow you to control the instantiation of your class.
20
INCTURE 2011 / 20
CLASS c1 DEFINITION.
PUBLIC SECTION. DATA: int TYPE I VALUE 10. METHODS display_int. ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD display_int. WRITE / int. ENDMETHOD. ENDCLASS. DATA : oref TYPE REF TO c1. START-OF-SELECTION. CREATE OBJECT oref. WRITE / oref-> int.
Classes are the templates of objects; actual objects must be created and referenced to be of use (except for the static components of a class) Reference variables (TYPE REF TO) contain references to objectsUsers can only access instance objects through reference variables. To use objects:
Declare reference variables.
21
INCTURE 2011 / 21
Self- Reference
CLASS c1 DEFINITION. PUBLIC SECTION. DATA: int TYPE I VALUE 10. METHODS display_int. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD display_int. DATA : int TYPE I VALUE 20. WRITE:/ int, ME->int. ENDMETHOD. ENDCLASS. DATA : oref TYPE REF TO c1. CREATE OBJECT oref. CALL METHOD oref-> display_int.
If an objects internally needs to provide its own reference. For example to another object, it can use the local reference variable ME. ME is predefined and always contains a reference to the address of its own object.
Multiple instantiation
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS meth. ENDCLASS. CLASS c1 IMPLEMENTATION. ENDCLASS.
DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c1. START-OF-SELECTION.
INCTURE 2011 / 23
23
Deleting Objects
oref1
oref2 TYPE REF TO c2.
9999
oref2
8888
oref1 = oref2.
oref1
8888
oref2
CLEAR oref1.
8888
oref1
oref2
8888
CLEAR oref2.
Object C2
Object C1
oref1 oref2
Object C2
24
INCTURE 2011 / 24
Functional Methods
RECEIVING r = a.
Instead of CALL METHOD, functional methods can be performed in expressions. A Functional method can have zero to many IMPORTING parameters and exactly one RETURNING parameter, that must be passed by value. A Functional method can be instance method or it can be static method.
25
INCTURE 2011 / 25
Pointer tables
DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c1, oref3 TYPE REF TO c1.
DATA: oref TYPE REF TO c1, oref_tab TYPE TABLE OF REF TO c1.
START-OF-SELECTION. DO 3 TIMES. CREATE OBJECT oref. APPEND oref TO oref_tab. ENDDO. LOOP AT oref_tab INTO oref. CALL METHOD oref->meth. ENDLOOP.
Pointer tables are used to store multiple instances of same class. This method reduces coding and more elegant against creating separate, separate object reference variables for storing every objects of the same class. Reference variables are handled like any other data object with an elementary data type.
26
INCTURE 2011 / 26
Instance, self-reference, and static method can all be called dynamically; the class name for static methods can also be determined dynamically: Variants: - oref->(method) - me->(method) - class=>(method) - (class)=>method - (class)=>(method) A methods parameters can be passed dynamically using PARAMETER-TABLE and EXCEPTION-TABLE additions to the CALL METHOD statement.
27
INCTURE 2011 / 27
Demonstration
Creating a local class with different components in different visibility sections and showing how to instantiate the class as well as how to access the instance and static components.
28
INCTURE 2011 / 28
Practice
Creating a local class with different components in different visibility sections and showing how to instantiate the class as well as how to access the instance and static components.
29
INCTURE 2011 / 29
Summary
Features of Object oriented programming are:
Abstraction Ecapsulation Inheritance Polymorphism Code Reuse
Classes and Objects are the basic building blocks of Object Oriented Programming When a real world entity is modeled into OOP world then it is known as Class, characteristics as attributes and functionality as methods. Objects is an instance of a Class. Classes can be of two types:
Global Class (Created using class builder (SE24) and stored in class repository as Class pool) Local Class (Created in any ABAP program)
30
INCTURE 2011 / 30
Questions
What is Object Oriented Programming ? What are the main advantages of Object Oriented Programming over Procedural Programming ? What is a Class? What is an Object? Which transaction we use to maintain global class? What is a constructor? What are the various visibility sections present in a ABAP class? What is the basic difference between static component and instance component? Can we access the static component of a class by the object name instead of the class name?
31
INCTURE 2011 / 31