Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The code below is an example to find out the name of an objects class programmatically using
RTTS classes:
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
METHODS: meth_a1.
ENDCLASS.
CLASS lcl_a IMPLEMENTATION.
METHOD meth_a1.
WRITE: /3 'From meth_a1 in class lcl_a'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_b DEFINITION INHERITING FROM lcl_a.
PUBLIC SECTION.
METHODS: meth_b1.
ENDCLASS.
CLASS lcl_b IMPLEMENTATION.
METHOD meth_b1.
WRITE: /3 'From meth_b1 in class lcl_b'.
ENDMETHOD.
ENDCLASS.
************************
DATA: a1 TYPE REF TO lcl_a,
b1 TYPE REF TO lcl_b,
typ_descr type ref to CL_ABAP_TYPEDESCR,
cl_name TYPE string.
START-OF-SELECTION.
CREATE OBJECT: a1, b1.
*& At the time when the below line of code gets executed the reference
*& variable a1 is pointing to an object of the class LCL_A
typ_descr = CL_ABAP_TYPEDESCR=>describe_by_object_ref( a1 ).
cl_name = typ_descr->get_relative_name( ).
WRITE /3 cl_name.
SKIP.
a1 = b1.
CLEAR b1.
*& At the time when the below line of code gets executed the reference
*& variable a1 is pointing to an object of the class LCL_B
typ_descr = CL_ABAP_TYPEDESCR=>describe_by_object_ref( a1 ).
cl_name = typ_descr->get_relative_name( ).
WRITE /3 cl_name.
The output of the above code will be:
LCL_A
LCL_B
Interfaces:
An interface is like a class definition but without any implementation of its own. All the components of an
interface are public by default. Public is the only visibility scope that is applicable to interface
components; for this reason visibility specification is not allowed in an interface definition.
The implementation can be provided in the classes that inherit from the interface. Classes that inherit
from an interface and provide the implementation are known as the implementation classes of the
interface. A class can inherit from multiple interfaces.
We can declare reference variables of an interface, but we cannot create an object of an interface. The
reference variable of an interface can hold the reference of the object of any of the classes that inherit
from the interface and provide implementation to the interface methods.
INTERFACE lif_message.
METHODS: show_message.
ENDINTERFACE.
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
METHODS: meth_one.
INTERFACES: lif_message.
ENDCLASS.
CLASS lcl_a IMPLEMENTATION.
METHOD meth_one.
WRITE: /2 'From method meth_one( )'.
ENDMETHOD.
METHOD lif_message~show_message.
WRITE: /2 'From method lif_message~show_message( )'.
ENDMETHOD.
ENDCLASS.
*******************************
START-OF-SELECTION.
DATA: a1 TYPE REF TO lcl_a,
if1 TYPE REF TO lif_message.
CREATE OBJECT a1.
a1->meth_one( ).
a1->lif_message~show_message( ).
SKIP 2.
CREATE OBJECT if1 TYPE lcl_a. " OR if1 = a1.
*& The meth_one defined in the implementing class lcl_a cannot be
*& accessed through the interface lif_message reference
* if1->meth_one( ).
*& The method show_message though implemented in the class lcl_a, its
*& definition has been inherited from the interface lcl_message, hence
PUBLIC SECTION.
INTERFACES: lif_add,
"Inheriting from multiple interfaces
lif_subtract.
aliases: get_sum for lif_add~get_sum,
get_difference for lif_subtract~get_difference.
ENDCLASS.
CLASS lcl_arithmetic IMPLEMENTATION.
METHOD get_sum.
r_sum = arg1 + arg2.
ENDMETHOD.
METHOD get_difference.
r_diff = arg1 - arg2.
ENDMETHOD.
ENDCLASS.
***********************
START-OF-SELECTION.
DATA: arith TYPE REF TO lcl_arithmetic,
res TYPE i.
CREATE OBJECT arith.
res = arith->get_sum( arg1 = 20 arg2 = 14 ).
WRITE: /2 'Sum:', res.
res = arith->get_difference( arg1 = 20 arg2 = 14 ).
WRITE: /2 'Difference:', res.
Storing objects of different classes implementing a given interface in an internal table of the
interface type:
INTERFACE ishape.
METHODS: getarea RETURNING value(p_area) TYPE f,
getcircumference RETURNING value(p_cirf) TYPE f.
ENDINTERFACE.
* A Circle Class that inherits from interface ISHAPE
CLASS ccircle DEFINITION.
PUBLIC SECTION.
INTERFACES ishape.
ALIASES: getarea FOR ishape~getarea,
getcircumference FOR ishape~getcircumference.
METHODS: constructor IMPORTING imc_rad TYPE f,
getradius RETURNING value(p_rad) TYPE f.
PRIVATE SECTION.
DATA rad TYPE f.
CONSTANTS pi TYPE f VALUE '3.141592365359'.
ENDCLASS.
CLASS ccircle IMPLEMENTATION.
METHOD constructor.
rad = imc_rad.
ENDMETHOD.
METHOD getradius.
p_rad = rad.
ENDMETHOD.
METHOD ishape~getarea.
p_area = 2 * pi * rad.
ENDMETHOD.
METHOD ishape~getcircumference.
p_cirf = pi * rad * rad.
ENDMETHOD.
ENDCLASS.
* A Square Class that inherits from interface ISHAPE
CLASS csquare DEFINITION.
PUBLIC SECTION.
INTERFACES ishape.
ALIASES: getarea FOR ishape~getarea,
getcircumference FOR ishape~getcircumference.
METHODS: constructor IMPORTING imc_side TYPE f,
getside RETURNING value(p_side) TYPE f.
PRIVATE SECTION.
DATA side TYPE f.
ENDCLASS.
CLASS csquare IMPLEMENTATION.
METHOD constructor.
side = imc_side.
ENDMETHOD.
METHOD ishape~getarea.
p_area = side * side.
ENDMETHOD.
METHOD ishape~getcircumference.
p_cirf = 4 * side.
ENDMETHOD.
METHOD getside.
p_side = side.
ENDMETHOD.
ENDCLASS.
*****************************************************
DATA: w_shape TYPE REF TO ishape,
" Ref-var of type ishape
t_shape TYPE TABLE OF REF TO ishape. "Array of Objects of type ishape
DATA: c1 TYPE REF TO ccircle,
s1 TYPE REF TO csquare,
c2 TYPE REF TO ccircle,
ENDMETHOD.
ENDCLASS.
"pvt_method
ENDCLASS.
If a class contains one or more abstract methods, the subclasses of the abstract class will have to
redefine it and provide the necessary implementation.
If a class is not abstract (i.e. a normal class), such class is known as a concrete class. A subclass of an
abstract class that provides implementation to all the inherited abstract methods is not an abstract class
and hence is a concrete class.
We cannot create objects of an abstract class, but we can declare reference variable for an abstract
class. Such reference variables can hold the reference of an object of abstract class subclass that is a
concrete class.
"get_uom
"lcl_2d_shape IMPLEMENTATION
"get_side
METHOD get_area.
r_area = side * side.
ENDMETHOD.
"get_area
ENDCLASS.
"lcl_square IMPLEMENTATION
CLASS lcl_circle DEFINITION INHERITING FROM lcl_2d_shape.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_uom TYPE char3
imc_rad TYPE i,
get_radius RETURNING value(r_rad) TYPE f,
get_area REDEFINITION.
PRIVATE SECTION.
DATA: rad TYPE i.
CONSTANTS: pi TYPE f VALUE '3.2857'.
ENDCLASS.
"lcl_circle DEFINITION
CLASS lcl_circle IMPLEMENTATION.
METHOD constructor.
super->constructor( imc_uom = imc_uom ).
rad = imc_rad.
ENDMETHOD.
"constructor
METHOD get_radius.
r_rad = rad.
ENDMETHOD.
"get_radius
METHOD get_area.
r_area = 2 * pi * rad.
ENDMETHOD.
"get_area
ENDCLASS.
"lcl_circle IMPLEMENTATION
***************************
START-OF-SELECTION.
DATA: shape2d TYPE REF TO lcl_2d_shape,
area TYPE f,
uom TYPE char3.
CREATE OBJECT shape2d TYPE lcl_circle EXPORTING imc_uom = 'cm'
imc_rad = 5.
area = shape2d->get_area( ).
uom = shape2d->get_uom( ).
WRITE: /3 'Area of circle:', area DECIMALS 4 EXPONENT 0, uom.
CREATE OBJECT shape2d TYPE lcl_square EXPORTING imc_uom = 'm'
imc_side = 6.
area = shape2d->get_area( ).
uom = shape2d->get_uom( ).
WRITE: /3 'Area of square:', area DECIMALS 4 EXPONENT 0, uom.
1. REPORT SQUAREROOT.
2. PARAMETERS: NUM TYPE P.
3. DATA: RES TYPE P DECIMALS 2.
4. START-OF-SELECTION.
5. RES = SQRT( NUM ).
6. WRITE: / 'Square root of', NUM, 'is', RES.
While executing the above report program; if a negative value is entered in the selection screen, an
exception occurs as square root cannot be calculated for a negative value and the execution results in
abnormal termination of the program. To be precise it is the 5 th line of code RES = SQRT( NUM ) which
results in the abnormal condition. The standard SAP class that represents this abnormal condition is
CX_SY_ARG_OUT_OF_DOMAIN. The code below explains how to use a TRY-ENDTRY block and a
CATCH statement to avoid abnormal termination and an alternative way to process the input.
REPORT SQUAREROOT.
PARAMETERS: NUM TYPE P.
DATA: RES TYPE P DECIMALS 2.
START-OF-SELECTION.
TRY.
RES = SQRT( NUM ).
CATCH CX_SY_ARG_OUT_OF_DOMAIN.
WRITE: / 'Cannot calculate square-root for a negative value'.
WRITE: / 'The entered -ve value has been negated again to make it +ve'.
NUM = NUM * -1.
RES = SQRT( NUM ).
ENDTRY.
WRITE: / 'Square root of', NUM, 'is', RES.
Multiple CATCH statements within a single TRY-ENDTRY block:
A TRY-ENDTRY block can contain multiple CATCH statements, where each catch statement deals with a
different exception condition.
1. REPORT DIVISION_AND_SQUARE.
2. PARAMETERS: num TYPE p OBLIGATORY, numerator
i. den TYPE p OBLIGATORY. denominator
3. DATA: res TYPE p DECIMALS 2.
4. START-OF-SELECTION.
5. RES = NUM / DEN.
6. WRITE: /3 NUM, '/', DEN, '=', RES.
7. *& Multiplication may result in arithmetic overflow error if the computed
8. *& value is too large to be stored in packed-decimal type
9. RES = NUM * NUM.
CX_SY_ARG_OUT_OF_DOMAIN
CX_ARITHMETIC_OVERFLOW
CX_ZERODIVIDE