Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives
Abstraction
Modeling real world entities and processes in a more natural way.
Ecapsulation
Hiding data and its related logic behind well defined interfaces.
Inheritance
Reusing attributes and methods while allowing for specialization.
Polymorphism
Simplifying by hiding varying implementations behind the same interface.
Code Reuse
Same code can be reused multiple times by using inheritance.
SAP Basis Release 4.5 delivered the first version of ABAP Objects.
SAP Basis Release 4.6 delivered complete version of ABAP Objects by
introducing ‘Inheritance’.
SAP Web Application Server 6.10/6.20 enhanced ABAP Objects with Friendship
and Object Services.
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 :
Functions of the box? (Methods)
What are the It can store things
characteristics of the
box? (Attributes) It can occupy space
Methods
Methods are the functionality of a
CLASS c1 DEFINITION. class , ABAP codes are written within
a method to incorporate the
PUBLIC SECTION.
functionality.
METHODS: do_something
Methods are processing blocks with a
IMPORTING ...i1 TYPE…
parameter interface very similar to
EXPORTING…e1 TYPE…
function modules.
CHANGING …c1 TYPE…
Methods are of two types:
EXCEPTIONS …en.
Standard Methods.
PRIVATE SECTION.
e.g. METHODS meth.
DATA: … Event handler methods:
ENDCLASS. METHODS meth FOR EVENT evt
CLASS c1 IMPLEMENTATION. OF class.
METHOD do_something. This type of methods are written to
trap events.
…
Methods are called with a CALL
ENDMETHOD.
METHOD statement.
ENDCLASS.
Constructors
CLASS c1 DEFINITION.
Classes are the templates of objects;
PUBLIC SECTION.
actual objects must be created and
DATA: int TYPE I VALUE ’10’. referenced to be of use (except for
METHODS display_int. the static components of a class)
ENDCLASS. Reference variables (TYPE REF TO)
CLASS c1 IMPLEMENTATION. contain references to objects- Users
METHOD display_int. can only access instance objects
WRITE / int. through reference variables.
ENDMETHOD.
ENDCLASS. To use objects:
DATA : oref TYPE REF TO c1. Declare reference variables.
START-OF-SELECTION. Create objects, assigning their
CREATE OBJECT oref. references.
WRITE / oref-> int. Use the object components
Self- Reference
CLASS c1 DEFINITION.
If an objects internally needs to
PUBLIC SECTION. provide its own reference. For
DATA: int TYPE I VALUE ’10’. example to another object, it can use
METHODS display_int. the local reference variable “ME”.
ENDCLASS. “ME” is predefined and always
CLASS c1 IMPLEMENTATION. contains a reference to the address
METHOD display_int. of its own object.
DATA : int TYPE I VALUE ’20’.
WRITE:/ int,
ME->int.
ENDMETHOD.
ENDCLASS.
DATA : oref TYPE REF TO c1.
Note : “ME” is equivalent to “THIS”
CREATE OBJECT oref.
pointer in C++.
CALL METHOD oref-> display_int.
Multiple instantiation
CLASS c1 DEFINITION.
Programs can instantiate multiple
PUBLIC SECTION.
objects of the same class.
METHODS meth.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
…
ENDCLASS.
Deleting Objects
… oref1
oref2 Object C2
8888
oref1 Object C1
CLEAR oref2. oref2 Object C2
Functional Methods
e.g.,
var = oref-> meth( i1 = a1….in =
an).
Pointer tables
DATA: oref1 TYPE REF TO c1, Pointer tables are used to store
oref2 TYPE REF TO c1, multiple instances of same
oref3 TYPE REF TO c1. class. This method reduces
coding and more elegant
DATA: oref TYPE REF TO c1, against creating separate,
oref_tab TYPE TABLE OF REF TO c1. separate object reference
variables for storing every
START-OF-SELECTION. objects of the same class.
… Reference variables are
DO 3 TIMES. handled like any other data
CREATE OBJECT oref. object with an elementary data
APPEND oref TO oref_tab. type.
ENDDO.
…
LOOP AT oref_tab INTO oref.
CALL METHOD oref->meth.
ENDLOOP.
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.
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.
Summary
Questions