You are on page 1of 9

Object Oriented Concepts

CLASS: Class is a blue print / Template of the object.


Object is a real-world entity.
A class mainly contains two sections.
1. Class Definition
2. Class Implementation

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>.

Syntax for method declaration:


1. Without parameters:
Methods <Method name>.

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.

Syntax for method Implementation:


Without parameters / With parameters:
Method <method name>.
Statements….
Endmethod.
Note:
1. If parameters list is declared at method declaration all of them can be used at method
implementation time. (With in the same class implementation)
2. No need to call the parameter list at implementation time
Method calling:
Without parameter list:
Call method <Object name><method name>

With parameter List:


Call method <Object name><method name> exporting …
Importing ….
Changing …

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 .

DATA o_ref TYPE REF TO cl.


CREATE OBJECT o_ref .

call METHOD o_ref->add_data


exporting v1 = p1
v2 = p2 .

call METHOD o_ref->display_data .

Output :

Enter the value and execute.


Class can have mainly two components:
1. Static Components.
2. Instance Components.
static components are declared with “class-data.”
EXAMPLE:

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 .

WRITE :/ 'Displaying....', o_ref->a .

WRITE : /'Displaying.....', cl=>b .

Output:

After executing we get the following screen.


INHERITANCE:
Derving a new class based on the existing class is called INHERITANCE.
Existing class is called Base class/parent class/super class.
Derving class is called derived class/child class/sub class.
Note:
1. Sub class can access all the components of super class which are under protected and public
sections.
2. Sub class cannot access the private components of super class.

Syntax of defining the sub class:


Class <subclass> definition inheriting from <superclass>.
Public/protected/private section.
…………………
Endclass.

Example:
REPORT Z9AM_INHERITANCE.

PARAMETERS : p_bukrs TYPE bukrs .

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 .

CLASS cl_2 DEFINITION INHERITING FROM cl .


PUBLIC SECTION.
* METHODs get_data.
METHODS display_data .
ENDCLASS .

CLASS cl_2 IMPLEMENTATION .


METHOD display_data.

LOOP AT it_ekpo INTO wa_ekpo.


WRITE : / wa_ekpo-matnr,
wa_ekpo-matkl.
ENDLOOP.
ENDMETHOD .
ENDCLASS .

START-OF-SELECTION.

DATA : o_ref1 TYPE REF TO cl,


o_ref2 TYPE REF TO cl_2.

CREATE OBJECT: o_ref1, o_ref2.

call METHOD o_ref2->get_data


EXPORTING
m_bukrs = p_bukrs .

CALL METHOD o_ref2->display_data( ).

OUTPUT: Execute and we can see the following screen

Execute, then we get the following output .


INTERFACE:
A method is declared only once and no implementation is maintained.
NOTE:
1. It is just declared once separately and not implemented.
2. In different classes different implementations are different implementations are maintained.
But name of the method is same.

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.

CLASS DataflairClass1 Definition.


PUBLIC Section.
Interfaces df_interface.
ENDCLASS.

CLASS DataflairClass2 Definition.


PUBLIC Section.
Interfaces df_interface.
ENDCLASS.

CLASS DataflairClass1 Implementation.


Method df_interface~Dataflairmethod1.
df_interface~text1 = 'DataflairClass 1 Interface method'.
Write / df_interface~text1.
EndMethod.
ENDCLASS.

CLASS DataflairClass2 Implementation.


Method df_interface~Dataflairmethod1.
df_interface~text1 = 'DataflairClass 2 Interface method'.
Write / df_interface~text1.
EndMethod.
ENDCLASS.

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

You might also like