Está en la página 1de 18

CLASS: When we define a class, we are essentially defining a new data type.

A data type defined as a


class is known as Reference Type.
When we define a structure, then too we are defining a new data type. A structure is a collection of
related fields, but a structure has no processing logic of its own (processing logic: code to assign, read
and/or manipulate data in the fields of that structure). A class is similar to a structure in a way that it can
hold a set of related fields, but in addition to this it can have its own processing logic in the form of
methods.
Two types of classes in ABAP:
Global Class: These types of classes are created using class builder tool and will be available globally
for all ABAP programs.
Local Class: These types of classes are created in an ABAP program and will ba available only to that
program where it defined.
A class in ABAP OOP is coded in two parts:
1. Class Definition.
2. Class Implementation.
A Class Definition contains declarations of fields and methods but not processing logic. The
processing logic is provided in Class Implementation. Therefore, a Class Definition must precede
Class Implementation, i.e., the definition has to be coded first and only then the implementation can be
coded.
Every class can have 3 sections:
1. Public
2. Protected
3. Private
These sections determine the visibility (access) levels of the components of a class (* Visibility/access
levels will be dealt with later.)
Members / Components of a Class: The contents of a class are referred to as members of a class or
components of a class, which include:
1. Attributes (also called as Data Components)
2. Methods
3. Interfaces and
4. Events.
All the above mentioned components of a class (except for Interfaces) can be declared in any of the three
sections (public, protected, & private sections) of a class. Interfaces can be placed only in public section.
Attributes / Data Components: Attributes of a class are the data fields (variables & constants) declared
in a class. Attributes of a class can be declared using the keywords DATA or CLASS-DATA (*the
difference between DATA or CLASS-DATA will be dealt with later.)
Methods: Methods are similar to subroutines or function modules in the sense that they can accept
parameters, contain some code (processing logic) to act on the parameters, except for that they are
contained in a class. Methods of a class can be declared using the keywords METHODS or CLASSMETHODS ( *the difference between the two will be dealt with later.)
Interfaces and Events will be dealt with later.
Public Section: There are no restrictions on the visibility / access of the components placed in public
ection. That is the public components are visible / accessible:
1. Within class in which the components are declared,
2. In the subclasses, and

3. Outside the class inheritance hierarchy.


(* Subclasses & Inheritance Hierarchy topics will be dealt with later)
Protected Section: There is one restriction on the components placed in protected section.
components of this section are visible / accessible:
1. Within class in which the components are declared,
2. In the subclasses, but
3. Not accessible outside the class inheritance hierarchy.

The

Private Section: The visibility of the components placed in private section is highly restricted as they are
visible only within the class and nowhere else.
Example: The class LCL_EMP defined below has only one section (public) and has only attributes
defined in it.
Syntax for creating objects:
CREATE OBJECT cref [TYPE class].
*--------------------------------------------------------------*
CLASS lcl_emp DEFINITION
*--------------------------------------------------------------CLASS lcl_emp DEFINITION.
* Declaration of data components in public section.
PUBLIC SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION
*--------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION
*--------------------------------------------------------------CLASS lcl_emp IMPLEMENTATION.
* No processing logic as no methods have been declared in the definition
ENDCLASS.
"lcl_emp IMPLEMENTATION
**********************************************
START-OF-SELECTION.
DATA: e1 TYPE REF TO lcl_emp,
e2 TYPE REF TO lcl_emp.
CREATE OBJECT: e1 TYPE lcl_emp, e2 TYPE lcl_emp.
*& CREATE OBJECT: e1, e2. This is also a valid syntax to create objects
e1->eno = 101.
e1->ename = 'Swaroop Kumar'.
e1->dno = 10.
WRITE: /3 'ENo:', e1->eno,
'EName:', e1->ename,
'DNo:', e1->dno.
e2->eno = 102.
e2->ename = 'Ajay Mohan'.
e2->dno = 20.

WRITE: /3 'ENo:', e2->eno,


'EName:', e2->ename,
'DNo:', e2->dno.
In the above program, we have defined a class that achieves nothing more than what can be achieved
with a structure. This is not the way a class is usually used. While designing a class, we should define
processing logic (methods to assign, read & manipulate data).
Report ZOOP_CLASS_WITH_METHODS
*--------------------------------------------------------------*
CLASS lcl_emp DEFINITION
*--------------------------------------------------------------CLASS lcl_emp DEFINITION.
PUBLIC SECTION.
*& It is good coding convention to prefix import parameters with prefix im_
METHODS: set_details IMPORTING im_eno TYPE i
im_ename TYPE string
im_dno TYPE i,
show_details.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION
*--------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION
*--------------------------------------------------------------CLASS lcl_emp IMPLEMENTATION.
METHOD set_details.
eno = im_eno.
ename = im_ename.
*& Let us assume that only 10 and 20 are valid department numbers.
*& If any other value is passed to the method parameter
*& do not assign such value.
IF im_dno = 10 OR im_dno = 20.
dno = im_dno.
ENDIF.
ENDMETHOD.
"set_details
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_emp IMPLEMENTATION
************************************************
START-OF-SELECTION.
DATA: e1 TYPE REF TO lcl_emp,
e2 TYPE REF TO lcl_emp,
e3 TYPE REF TO lcl_emp.
*& The next line of code (has been commented) results in runtime error as the
*& object is not yet created.

*e1->show_details( ).
CREATE OBJECT e1.
CREATE OBJECT e2.
e1->set_details( EXPORTING im_eno = 101
im_ename = 'Ajay'
im_dno = 20 ).
e2->set_details( EXPORTING im_eno = 102
im_ename = 'Vijay'
im_dno = 45 ).
e1->show_details( ).
e2->show_details( ).
skip.
*& Though the set_details method doesn't allow us to assign a wrong dept number,
*& we can still assign a wrong value to the data member 'dno' as the data components
*& are declared in 'PUBLIC SECTION', and hence are accessible outside the class

e2->dno = 45.
e2->show_details( ).
skip.
*& The line of code below will result in the reference variable e3 point to the same
*& object that the reference variable e1 is pointing to. No new object is created
*& for e3.

e3 = e1.
e3->show_details( ).
In the above program though care has been taken in the set_details method that no wrong department
number should be assigned to the attribute dno, we could still violate this by directly accessing the
attribute and assigning a wrong value to it. To avoid such scenarios, we will have to declare the data
components in the PRIVATE SECTION. The components of a class declared in private section are like a
private property of that class and hence are accessible only with that class but not outside. In contrast to
the private section, the components declared in public section are like a public property and hence are
accessible everywhere, i.e., within the class and also outside the class.
REPORT ZOOP_PUBLIC_PRIVATE.
*--------------------------------------------------------------*
CLASS lcl_emp DEFINITION
*--------------------------------------------------------------CLASS lcl_emp DEFINITION.
PUBLIC SECTION.
METHODS: set_details IMPORTING im_eno TYPE i
im_ename TYPE string
im_dno TYPE i,
show_details.
PRIVATE SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION
*--------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION
*---------------------------------------------------------------

CLASS lcl_emp IMPLEMENTATION.


METHOD set_details.
eno = im_eno.
ename = im_ename.
IF im_dno = 10 OR im_dno = 20.
dno = im_dno.
ENDIF.
ENDMETHOD.
"set_details
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_emp IMPLEMENTATION
************************************************
START-OF-SELECTION.
DATA: e1 TYPE REF TO lcl_emp,
e2 TYPE REF TO lcl_emp.
CREATE OBJECT: e1, e2.
e1->set_details( EXPORTING im_eno = 101
im_ename = 'Ajay'
im_dno = 20 ).
e2->set_details( EXPORTING im_eno = 102
im_ename = 'Vijay'
im_dno = 45 ).
e1->show_details( ).
e2->show_details( ).
*& Any attempt to access the private components of a class outside the class *& will result
in syntax (compile time) error.
*e2->dno = 45.
CONSTRUCTOR: Constructor is a special method that gets implicitly called immediately after an object is
created using the CREATE OBJECT command. There is no need to explicitly call the constructor
method; in fact, the constructor method cannot be called explicitly. The name of this method has to be
CONSTRUCTOR.
The CREATE OBJECT command performs 3 tasks:
1. Allocates the memory required for an objects attributes (* Instance attributes only) .
2. Calls the method with the name constructor of the corresponding class.
3. Returns the reference of the object to the associated reference variable.

REPORT ZOOP_CONSTRUCTOR.
*----------------------------------------------------------------------*
*
CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
PUBLIC SECTION.
*& It is good coding convention to prefix import parameters of a constructor
*& with a prefix imc_
METHODS: constructor IMPORTING imc_eno TYPE i

imc_ename TYPE string


imc_dno TYPE i,
show_details.
PRIVATE SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION.
METHOD constructor.
eno = imc_eno.
ename = imc_ename.
dno = imc_dno.
ENDMETHOD.
"constructor
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_emp IMPLEMENTATION
************************************************
START-OF-SELECTION.
DATA: e1 TYPE REF TO lcl_emp,
e2 TYPE REF TO lcl_emp,
e3 TYPE REF TO lcl_emp.
CREATE OBJECT e1 EXPORTING imc_eno = 101
imc_ename = 'Ajay'
imc_dno = 20.
CREATE OBJECT e2 EXPORTING imc_eno = 102
imc_ename = 'Vijay'
imc_dno = 10.
e1->show_details( ).
e2->show_details( ).
e3 = e1.
e3->show_details( ).
Inheritance:
Inheritance is a relationship in which one class (the sub class) inherits all the main characteristics of
another class (the Super class).
The sub class can also add new components (attributes, methods and so on) and replace or extend
inherited methods with its own implementations. The addition INHERITING FROM is used in the class
definition to indicate from which other class the current class is inheriting from.

REPORT ZOOP_INHERITANCE.
*----------------------------------------------------------------------*
*
CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
PUBLIC SECTION.
METHODS: set_details IMPORTING im_eno TYPE i
im_ename TYPE string
im_dno TYPE i,
show_details.
PRIVATE SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION

*----------------------------------------------------------------------*
*
CLASS lcl_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION.
METHOD set_details.
eno = im_eno.
ename = im_ename.
IF im_dno = 10 OR im_dno = 20.
dno = im_dno.
ENDIF.
ENDMETHOD.
"set_details
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_emp IMPLEMENTATION
*----------------------------------------------------------------------*
*
CLASS lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp DEFINITION INHERITING FROM lcl_emp.
PUBLIC SECTION.
METHODS: set_h_wage IMPORTING im_h_wage TYPE i,
show_h_wage.
PRIVATE SECTION.
DATA: h_wage TYPE i.
ENDCLASS.
"lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_contract_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp IMPLEMENTATION.

METHOD set_h_wage.
h_wage = im_h_wage.
ENDMETHOD.

"set_h_wage

METHOD show_h_wage.
WRITE: 'H Wage:', h_wage.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_contract_emp IMPLEMENTATION
**************************
START-OF-SELECTION.
DATA: ce TYPE REF TO lcl_contract_emp.
CREATE OBJECT: ce.
ce->set_details( EXPORTING im_eno = 104
im_ename = 'Raj'
im_dno = 10 ).
ce->set_h_wage( EXPORTING im_h_wage = 200 ).
ce->show_details( ).
ce->show_h_wage( ).
In the above program to display the details of a Contract Employee object we have to invoke two
methods. The show_details method inherited from the super class LCL_EMP, and the show_h_wage
method that has been newly defined in the subclass LCL_CONTRACT_EMP. This is so because the
implementation of the show_details method as provided in the super class can display only the attributes
defined in the super class, but not that of the subclass. Therefore, to display the hourly wage information
we had to add a new method show_h_wage in the subclass. To avoid coding a new method, the
show_details method which has been inherited in the subclass, from the super class, has to be redefined
so as to display the values in the attributes of both the superclass and subclass.
Method Overriding: Redefining a method in the subclass, which has been inherited from its superclass,
is called method overriding. While redefining an inherited method the functionality defined in the
superclass can be retained as is and some more additional functionality can be provided, or the inherited
functionality can be nullified and altogether a new functionality can be provided.
NOTE:
1) While overriding a method in the subclass, the signature of the method has to exactly match with
the signature of the corresponding method in the superclass.
2) In the subclass method, during redefinition (overriding), if we have to access the implementation
of the corresponding method in the superclass we will have to use the keyword SUPER. The
keyword SUPER is a reference (pseudo reference) to the current class superclass
implementation.
3) While overriding a method if the keyword SUPER is not used, it means that the methods
functionality as implemented in the superclass is being nullified in the subclass and an altogether
new functionality is being provided.
Method Signature: The signature of a method includes the number of parameters, type of parameters
(importing, exporting and/or changing; and the data types), and the sequence of parameters.
REPORT ZOOP_OVERRIDING.
*----------------------------------------------------------------------*
*
CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
PUBLIC SECTION.

METHODS: set_details IMPORTING im_eno TYPE i


im_ename TYPE string
im_dno TYPE i,
show_details.
PRIVATE SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION.
METHOD set_details.
eno = im_eno.
ename = im_ename.
IF im_dno = 10 OR im_dno = 20.
dno = im_dno.
ENDIF.
ENDMETHOD.
"set_details
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_emp IMPLEMENTATION
*----------------------------------------------------------------------*
*
CLASS lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp DEFINITION INHERITING FROM lcl_emp.
PUBLIC SECTION.
METHODS: set_h_wage IMPORTING im_h_wage TYPE i,
show_details REDEFINITION.
PRIVATE SECTION.
DATA: h_wage TYPE i.
ENDCLASS.
"lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_contract_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp IMPLEMENTATION.
METHOD set_h_wage.
h_wage = im_h_wage.
ENDMETHOD.
"set_h_wage
METHOD show_details.
super->show_details( ). retaining the implementation provided in superclass
WRITE: 'H Wage:', h_wage. additional functionality being add in subclass
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_contract_emp IMPLEMENTATION
**************************
START-OF-SELECTION.

DATA: ce TYPE REF TO lcl_contract_emp.


CREATE OBJECT: ce.
ce->set_details( EXPORTING im_eno = 104
im_ename = 'Raj'
im_dno = 10 ).
ce->set_h_wage( EXPORTING im_h_wage = 200 ).
ce->show_details( ).
In the above program by overriding the show_details method in the LCL_CONTRACT_EMP class, we
extended the inherited functionality of that method. But, if we make a similar attempt to over the
set_details method, it will not be possible. This is because in the subclass LCL_CONTRACT_EMP the
set_details method will have to accept 4 parameters (including h_wage), where as the set_details
method in the superclass LCL_EMP accepts only 3 parameters. By adding the 4 th parameter the method
signature will change, and this is not allowed while overriding a method. This drawback can be overcome
by using a constructor instead of the set_details method, as the signature of a constructor in a subclass
need not necessarily match with the constructor of its superclass.
REPORT ZOOP_SUBCLASS_AND_CONSTRUCTOR.
*----------------------------------------------------------------------*
*
CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_eno TYPE i
imc_ename TYPE string
imc_dno TYPE i,
show_details.
PRIVATE SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
"lcl_emp DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp IMPLEMENTATION.
METHOD constructor.
eno = imc_eno.
ename = imc_ename.
IF imc_dno = 10 OR imc_dno = 20.
dno = imc_dno.
ENDIF.
ENDMETHOD.
"constructor
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_emp IMPLEMENTATION

*----------------------------------------------------------------------*
*
CLASS lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp DEFINITION INHERITING FROM lcl_emp.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_eno TYPE i
imc_ename TYPE string
imc_dno TYPE i
imc_h_wage TYPE i,
show_details REDEFINITION,
get_total_wage importing im_hrs_worked type i
exporting ex_t_wage type i.
PRIVATE SECTION.
DATA: h_wage TYPE i.
ENDCLASS.
"lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_contract_emp IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp IMPLEMENTATION.
METHOD constructor.
super->constructor( EXPORTING imc_eno = imc_eno
imc_ename = imc_ename
imc_dno = imc_dno ).
h_wage = imc_h_wage.
ENDMETHOD.
"constructor
METHOD show_details.
super->show_details( ).
WRITE: 'H Wage:', h_wage.
ENDMETHOD.
"show_details
METHOD get_total_wage.
ex_t_wage = im_hrs_worked * h_wage.
ENDMETHOD.
ENDCLASS.
"lcl_contract_emp IMPLEMENTATION
**************************
START-OF-SELECTION.
DATA: ce TYPE REF TO lcl_contract_emp,
t_wage type i.
CREATE OBJECT: ce EXPORTING imc_eno = 104
imc_ename = 'Raj'
imc_dno = 10
imc_h_wage = 200.
ce->show_details( ).
ce->get_total_wage( EXPORTING im_hrs_worked = 36
IMPORTING ex_t_wage = t_wage ).
write: /3 'Total wage:', t_wage.
Notes about keyword SUPER:
1. In the constructor of a subclass it is mandatory to invoke the superclass constructor in the very
first line of the constructors code using the keyword SUPER.
2. In other methods the SUPER can be used in any line.

3. SUPER can be used in only the methods that are being redefined. Moreover, say an inherited
method meth_x is being redefined in a subclass; such method can access only the
implementation of meth_x of its superclass using SUPER, but not the other method
implementations from its superclass.

Functional Methods:
Methods with RETURNING parameters are known as functional methods (and those without
RETURNING parameters are known as non-functional methods.)

A method defined with RETURNING parameters cannot have EXPORTING & CHANGING
parameters.
RETURNING parameters are always passed by value and not by reference.
Functional methods can be called in assignment operation, and also as part of an expression
(logical expressions & arithmetic expressions), which is not possible with non-functional methods.
However, a functional method cannot be called as part of WRITE statement.

REPORT zoop_functional.

*----------------------------------------------------------------------*
*
CLASS lcl_arithmetic DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_arithmetic DEFINITION.
PUBLIC SECTION.
METHODS: get_sum IMPORTING im_p1 TYPE i
im_p2 TYPE i
EXPORTING ex_sum TYPE i,
get_diff IMPORTING im_p1 TYPE i
im_p2 TYPE i
RETURNING value(r_diff) TYPE i.
ENDCLASS.
"lcl_arithmetic DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_arithmetic IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_arithmetic IMPLEMENTATION.
METHOD get_sum.
ex_sum = im_p1 + im_p2.
ENDMETHOD.
"get_sum
METHOD get_diff.
r_diff = im_p1 - im_p2.
ENDMETHOD.
"get_diff
ENDCLASS.
"lcl_arithmetic IMPLEMENTATION
********************************
START-OF-SELECTION.
DATA: arith TYPE REF TO lcl_arithmetic,
res TYPE i.
CREATE OBJECT arith.
*& Syntax-1 to call a non-functional method\
* arith->get_sum( exporting im_p1 = 20
*
im_p2 = 40
*
importing ex_sum = res ).
*& Syntax-2 to call a non-functional method

CALL METHOD arith->get_sum


EXPORTING
im_p1 = 20
im_p2 = 40
IMPORTING
ex_sum = res.
WRITE: / 'Sum:', res.
*& Syntax-1 to call a functional method
* res = arith->get_diff( im_p1 = 40
*
im_p2 = 22 ).
*& Syntax-2 to call a functional method
* arith->get_diff( EXPORTING im_p1 = 40
*
im_p2 = 22
*
RECEIVING r_diff = res ).
*& Syntax-3 to call a functional method
CALL METHOD arith->get_diff
EXPORTING
im_p1 = 40
im_p2 = 22
RECEIVING
r_diff = res.
WRITE: / 'Difference:', res.

ME: The keyword ME refers to the current object of the current class.
*----------------------------------------------------------------------*
*
CLASS lcl_student DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_student DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_id TYPE i
imc_name TYPE string,
show_details.
PRIVATE SECTION.
DATA: id TYPE i,
name TYPE string.
ENDCLASS.
"lcl_student DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_student IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_student IMPLEMENTATION.
METHOD constructor.
id = imc_id.
name = imc_name.
ENDMETHOD.
"constructor
METHOD show_details.
WRITE: /3 'ID:', id,
'Name:', name.
ENDMETHOD.

"show_details

ENDCLASS.

"lcl_student IMPLEMENTATION

The class lcl_student can also be defined as below:


*----------------------------------------------------------------------*
*
CLASS lcl_student DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_student DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING id TYPE i
name TYPE string,
show_details.
PRIVATE SECTION.
DATA: id TYPE i,
name TYPE string.
ENDCLASS.
"lcl_student DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_student IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_student IMPLEMENTATION.
METHOD constructor.
ME->id = id.
ME->name = name.
ENDMETHOD.
"constructor
METHOD show_details.
WRITE: /3 'ID:', id,
'Name:', name.
ENDMETHOD.
"show_details
ENDCLASS.
"lcl_student IMPLEMENTATION

CLASS-DATA & CLASS-METHODS


Attributes and methods declared CLASS-DATA and CLASS-METHODS, respectively, are known as class
components or static components, while those declared DATA and METHODS are known as instance
components of non-static components.
Notes about class & instance components:
1. A class member can be accessed in two ways:
Through the object reference (only when the reference variable points to an object of the class).
ref_variable->static-member
Through the class name (can be accessed before and also after an object of the class has been
created).
class_name=>static_member
2. The implementation of a class is loaded only once in the memory, which is referred to as ClassMemory; while for each object of a given class that has been created with CREATE OBJECT
command, memory is allocated separately; which is referred to as Instance-Memory.
3. Each class attribute occupies space in the class memory and hence there can be only one copy
of each class attribute; and the same copy is shared between the class implementation and all
the objects of that class.

4. The class implementation is loaded into the memory only once and that too when the class is
referred to for the first time. For this reason the class components can be accessed even before
an object of that class is created, and this can be done by accessing the class components
through the class name.
class_name=>static_member
5. Class methods can access other class components of the class but cannot access the instance
components. Which means a method defined as CLASS-METHOD can access only the
attributes that have been defined as CLASS-DATA and the methods that have defined as CLASSMETHODS, but not those defined as DATA and METHODS. On the other hand, instance
methods can access both class components as well as instance components of the class.

REPORT ZOOP_CLASS_INSTANCE_COMPONENTS.
*----------------------------------------------------------------------*
*
CLASS lcl_student DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_student DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_id TYPE i
imc_name TYPE string,
show_details.
CLASS-METHODS: get_count RETURNING value(cnt) TYPE i.
PRIVATE SECTION.
DATA: id TYPE i,
name TYPE string.
CLASS-DATA: count TYPE i. "Count of student objects created
ENDCLASS.
"lcl_student DEFINITION
*----------------------------------------------------------------------*
*
CLASS lcl_student IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_student IMPLEMENTATION.
METHOD constructor.
ADD 1 TO count.
id = imc_id.
name = imc_name.
ENDMETHOD.
"constructor
METHOD show_details.
WRITE: /3 'ID:', id,
'Name:', name.
ENDMETHOD.
METHOD get_count.
cnt = count.
ENDMETHOD.
ENDCLASS.

"show_details

"get_count
"lcl_student IMPLEMENTATION

************************************************
START-OF-SELECTION.
DATA: stu_count TYPE i.

stu_count = lcl_student=>get_count( ).
WRITE: /3 'Count before creating any objects:', stu_count.
DATA: s1 type ref to lcl_student,
s2 type ref to lcl_student.
CREATE OBJECT: s1 exporting imc_id = 101 imc_name = 'Vijay Anand'.
stu_count = lcl_student=>get_count( ).
WRITE: /3 'Count after creating first object:', stu_count.
CREATE OBJECT: s2 exporting imc_id = 102 imc_name = 'Swaroop Kumar'.
stu_count = lcl_student=>get_count( ).
WRITE: /3 'Count after creating second object:', stu_count.
skip 2.
stu_count
WRITE: /3
stu_count
WRITE: /3
stu_count
WRITE: /3

= s1->get_count( ).
'Count value thru s1->get_count( ):', stu_count.
= s2->get_count( ).
'Count value thru s2->get_count( ):', stu_count.
= lcl_student=>get_count( ).
'Count value thru lcl_student=>get_count( ):', stu_count.

Dereferencing objects and Garbage Collection:


1. DATA: s1 type ref to lcl_student,
2.
s2 type ref to lcl_student.
3. CREATE OBJECT: s1 exporting imc_id = 101 imc_name = 'Vijay Anand'.
4. CREATE OBJECT: s2 exporting imc_id = 102 imc_name = 'Swaroop Kumar'.
5. s1 = s2.
In the above piece of code the lines 1 & 2 declare two reference variables (ref-var) names s1 and s2. At
this stage no objects have yet been created. When the line-3 gets executed an object of class lcl_student
is created with id = 101, and that object is now being pointed by the ref-var s1. Similarly, when line-4 gets
executed an object of class lcl_student with id = 102 gets created the reference of which is held by ref-var
s2. But, when the line-5 gets executed the reference of the second object held by s2 get copied into s1.
Therefore, at this stage both the ref-vars s1 and s2 are pointing the same object, the one with id = 102.
What about the first object with id = 101? This object now has no ref-var pointing to it, which means that
this object has now been de-referenced. All the objects which have been de-referenced, i.e, the objects
which are no longer being referred to by any ref-var, are marked for garbage collection. Garbage
Collector is a service that is up and running continuously on the SAP server that keeps looking for objects
that have been de-referenced (marked for garbage collection) and releases (frees) the memory
associated with such objects.

State & Behaviour:


Every object has a state and behaviour.
The term STATE refers to the values held by the data fields of an object. As class attributes occupy
space in class-memory and not in the object-memory, they are not considered part of the state of an
object. It is the data (values) held by the instance components, which occupy space in object-memory
and are allocated separately for each object created, that determine the state of an object.

If exactly identical values are assigned to the corresponding data fields of two or more objects of the
same class, then such objects are said to be in same state.
Example:
DATA: s1 type ref to lcl_student,
s2 type ref to lcl_student.
CREATE OBJECT: s1 exporting imc_id = 101 imc_name = 'Vijay'.
CREATE OBJECT: s2 exporting imc_id = 101 imc_name = 'Vijay'.
In the above piece of code the objects referred to by the reference variables s1 and s2 are two different
objects, but the two objects s1 and s2 are said to have the same STATE.
The term BEHAVIOUR refers to the operations that can be performed (processing logic that can be
invoked) through an object reference. As the processing logic in a class is implemented in its methods, it
is the different methods available in a class that determines the behaviour of its objects.
Passing a Structure or Internal Table reference to a method in a class:
TYPES: BEGIN OF ty_line,
f1 TYPE i,
f2 TYPE i,
END OF ty_line.
CLASS lcl_myclass DEFINITION.
PUBLIC SECTION.
METHODS: populate_structure CHANGING s_line TYPE ty_line,
populate_itab CHANGING it_tab TYPE STANDARD TABLE.
ENDCLASS.
"lcl_myclass DEFINITION
CLASS lcl_myclass IMPLEMENTATION.
METHOD populate_structure.
s_line-f1 = 22.
s_line-f2 = 222.
ENDMETHOD.
"populate_structure
METHOD populate_itab.
DATA: l_line TYPE ty_line.
DO 5 TIMES.
l_line-f1 = sy-index * 10.
l_line-f2 = sy-index * 100.
APPEND l_line TO it_tab.
CLEAR l_line.
ENDDO.
ENDMETHOD.
"populate_itab
ENDCLASS.
"lcl_myclass IMPLEMENTATION
**********************
DATA: line TYPE ty_line,
itab TYPE TABLE OF ty_line WITH HEADER LINE,
mc TYPE REF TO lcl_myclass.
START-OF-SELECTION.
CREATE OBJECT mc.

mc->populate_structure( CHANGING s_line = line ).


WRITE: /2 line-f1, line-f2.
ULINE.
mc->populate_itab( CHANGING it_tab = itab[] ).
LOOP AT itab.
WRITE: / itab-f1, itab-f2.
ENDLOOP.

También podría gustarte