Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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.
*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
*---------------------------------------------------------------
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
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.
*----------------------------------------------------------------------*
*
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
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
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.
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.