Professional Documents
Culture Documents
Class Definition: Class definition contains all declarations of components and visibility sections.
Components of Class:
1. Attributes
2. Methods
3. Events
4. Interfaces
Components of Class:
Attributes:
Under attributes variables/data objects, work areas, Internal tables will come. These attributes are
declared with proper visibility.
Methods:
It contains the actual functionality to be executed.
Events:
Events are used to handle the methods of other classes.
Interfaces:
It is collection of method declarations only.
Visibility Sections:
We have totally three visibility sections.
1. Public section
2. Protected Section
3. Private section
Public Section: We can access all the attributes with in the class and outside the class also.
Protected Section: We can access the attributes within the class and in derived classes also.
Protected Section: We can access only within the class. Not outside the class.
Note:
1. From the class definition any number of objects can be created.
2. Name of the objects must be different from one another .
Syntax of a class
Class definition:
Class <class_name> definition.
Public/protected/Private
Statement1…
Statement 2... Declare the components of the class.
Endclass.
NOTE: We must give a visibility section.
Class Implementation:
All the methods are implemented in the class.
Class <class_name> Implementation.
Logic for methods….
Endclass.
Note:
1. Without object we cannot access the method of a class (In case of Instance methods)
2. We can access the methods with class name (In case of static methods)
Reference variable and object creation:
Syntax for Reference variable of a class:
DATA: < Reference variable> TYPE REF TO <Class name>.
Syntax for creating the object of a class:
Create object <Reference variable>.
2. With parameters:
Methods <Method name> Importing <p1> type …
<p2> type...
…….
exporting <p3> type …
<p4> type...
……
changing <p5> type …
<p6> type...
NOTE:
1. Importing, exporting, and changing are the parameters list. The parameters can be used
in the method implementation.
2. Parameters list is not mandatory.
Note:
1. At the time of calling parameters list of method must be called.
2. Creation of object and reference variable must be after START-OF-SELECTION event.
EXAMPLE:
Addition of two numbers using OOPS
CLASS cl DEFINITION .
PUBLIC SECTION.
DATA : a TYPE i,
b TYPE i,
res TYPE i .
METHODS: add_data IMPORTING v1 TYPE i
v2 TYPE i,
display_data .
ENDCLASS .
CLASS cl IMPLEMENTATION .
METHOD add_data.
a = v1 .
b = v2 .
res = a + b .
ENDMETHOD.
METHOD display_data .
write: 'result is', res .
ENDMethod .
ENDCLASS .
*==Screen declaration
PARAMETERS : p1 TYPE i,
p2 TYPE i .
START-OF-SELECTION .
Output :
CLASS cl DEFINITION.
PUBLIC SECTION.
DATA a type char20 value 'Instance_variable'.
CLASS-DATA b TYPE char20 value 'static_variable'.
ENDCLASS.
START-OF-SELECTION .
DATA : o_ref TYPE REF TO cl .
create OBJECT o_ref .
Output:
Example:
REPORT Z9AM_INHERITANCE.
CLASS cl DEFINITION .
PUBLIC SECTION.
TYPES :BEGIN OF ty_ekpo,
matnr TYPE matnr,
MATKL TYPE MATKL,
END OF ty_ekpo.
DATA : wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE TABLE OF ty_ekpo.
METHODS get_data IMPORTING m_bukrs TYPE bukrs .
ENDCLASS .
CLASS cl IMPLEMENTATION .
METHOD get_data.
SELECT matnr matkl FROM ekpo INTO TABLE it_ekpo WHERE bukrs eq p_bukrs
.
ENDMETHOD .
ENDCLASS .
START-OF-SELECTION.
EXAMPLE:
Explanation for the below example:
In the following example Interface (df_interface) is declared and inside only one time method
(Dataflairmethod1.) is maintained. But in class1 and class2 the method (Dataflairmethod1) is
differently implemented in the following example.
Report ZDATAFLAIR_ENCAPSULATION.
Interface df_interface.
Data text1 Type char35.
Methods Dataflairmethod1.
EndInterface.
Start-Of-Selection.
Data: DataflairObject1 Type Ref To DataflairClass1,
DataflairObject2 Type Ref To DataflairClass2.
Create Object: DataflairObject1, DataflairObject2.
CALL Method: DataflairObject1→df_interface~Dataflairmethod1,
DataflairObject2→df_interface~Dataflairmethod1.
OUTPUT:
DataflairClass 1 Interface method
DataflairClass 2 Interface method