You are on page 1of 17

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 si!ilar to a structure in a way that it can
hold a set of related fields, but in addition to this it can ha"e its own processing logic in the for! of
!ethods.
Two types of classes in ABAP:
Global Class: These types of classes are created using class builder tool and will be a"ailable globally
for all A#A$ progra!s.
Local Class: These types of classes are created in an A#A$ progra! and will ba a"ailable only to that
progra! where it defined.
A class in A#A$ %%$ is coded in two parts:
1. Class Definition.
2. Class Implementation.
A Class Definition contains declarations of fields and !ethods but not processing logic. The
processing logic is pro"ided in Class Implementation. Therefore, a &lass 'efinition !ust precede
&lass (!ple!entation, i.e., the definition has to be coded first and only then the i!ple!entation can be
coded.
)"ery class can ha"e * sections:
1. Pblic
2. P!otecte"
#. P!i$ate
These sections deter!ine the "isibility (access le"els of the co!ponents of a class (+ ,isibility-access
le"els will be dealt with later.
%embe!s & Components of a Class: The contents of a class are referred to as !e!bers of a class or
co!ponents of a class, which include:
.. Attributes (also called as 'ata &o!ponents
/. 0ethods
*. (nterfaces and
1. )"ents.
All the abo"e !entioned co!ponents of a class (e2cept for (nterfaces can be declared in any of the three
sections (public, protected, 3 pri"ate sections of a class. (nterfaces can be placed only in public section.
Att!ibtes & Data Components: Attributes of a class are the data fields ("ariables 3 constants declared
in a class. Attributes of a class can be declared using the keywords 'ATA or &4A556'ATA (+the
difference between 'ATA or &4A556'ATA will be dealt with later.
%et'o"s: 0ethods are si!ilar to subroutines or function !odules in the sense that they can accept
para!eters, contain so!e code (processing logic to act on the para!eters, e2cept for that they are
contained in a class. 0ethods of a class can be declared using the keywords 0)T7%'5 or &4A556
0)T7%'5 ( +the difference between the two will be dealt with later.
(nterfaces and )"ents will be dealt with later.
Pblic Section: There are no restrictions on the "isibility - access of the co!ponents placed in public
ection. That is the public co!ponents are "isible - accessible:
.. Within class in which the co!ponents are declared,
/. (n the subclasses, and
*. %utside the class inheritance hierarchy.
(+ 5ubclasses 3 (nheritance 7ierarchy topics will be dealt with later
P!otecte" Section: There is one restriction on the co!ponents placed in protected section. The
co!ponents of this section are "isible - accessible:
.. Within class in which the co!ponents are declared,
/. (n the subclasses, but
*. (ot accessible outside the class inheritance hierarchy.
P!i$ate Section: The "isibility of the co!ponents placed in pri"ate section is highly restricted as they are
"isible only within the class and nowhere else.
)2a!ple: The class 4&48)0$ defined below has only one section (public and has only attributes
defined in it.
Synta) fo! c!eatin* ob+ects:
&R)AT) %#9)&T cref :T;$) class<.
*---------------------------------------------------------------
* CLASS lcl_emp DEFINITION
*---------------------------------------------------------------
CLASS lcl_emp DEFINITION.
* Declaration of data components in pulic section.
!"#LIC SECTION.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*---------------------------------------------------------------
* CLASS lcl_emp I)!LE)ENTATION
*---------------------------------------------------------------
CLASS lcl_emp I)!LE)ENTATION.
* No processin' lo'ic as no met*ods *a+e een declared in t*e de,nition
ENDCLASS. (lcl_emp I)!LE)ENTATION
**********************************************
STA-T-OF-SELECTION.
DATA$ e. T%!E -EF TO lcl_emp&
e/ T%!E -EF TO lcl_emp.
C-EATE O#0ECT$ e. T%!E lcl_emp& e/ T%!E lcl_emp.
*1 C-EATE O#0ECT$ e.& e/. 2T*is is also a +alid s3nta4 to create o5ects
e.-6eno 7 .8..
e.-6ename 7 9S:aroop ;umar9.
e.-6dno 7 .8.
<-ITE$ => 9ENo$9& e.-6eno&
9EName$9& e.-6ename&
9DNo$9& e.-6dno.
e/-6eno 7 .8/.
e/-6ename 7 9A5a3 )o*an9.
e/-6dno 7 /8.
<-ITE$ => 9ENo$9& e/-6eno&
9EName$9& e/-6ename&
9DNo$9& e/-6dno.
(n the abo"e progra!, we ha"e defined a class that achie"es nothing !ore than what can be achie"ed
with a structure. This is not the way a class is usually used. While designing a class, we should define
processing logic (!ethods to assign, read 3 !anipulate data.
-eport ?OO!_CLASS_<IT@_)ET@ODS
*---------------------------------------------------------------
* CLASS lcl_emp DEFINITION
*---------------------------------------------------------------
CLASS lcl_emp DEFINITION.
!"#LIC SECTION.
*1 It is 'ood codin' con+ention to pre,4 import parameters :it* pre,4 im_
)ET@ODS$ set_details I)!O-TINA im_eno T%!E i
im_ename T%!E strin'
im_dno T%!E i&
s*o:_details.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*---------------------------------------------------------------
* CLASS lcl_emp I)!LE)ENTATION
*---------------------------------------------------------------
CLASS lcl_emp I)!LE)ENTATION.
)ET@OD set_details.
eno 7 im_eno.
ename 7 im_ename.
*1 Let us assume t*at onl3 .8 and /8 are +alid department numers.
*1 If an3 ot*er +alue is passed to t*e met*od parameter
*1 do not assi'n suc* +alue.
IF im_dno 7 .8 O- im_dno 7 /8.
dno 7 im_dno.
ENDIF.
END)ET@OD. (set_details
)ET@OD s*o:_details.
<-ITE$ => 9ENo$9& eno&
9EName$9& ename&
9DNo$9& dno.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_emp I)!LE)ENTATION
************************************************
STA-T-OF-SELECTION.
DATA$ e. T%!E -EF TO lcl_emp&
e/ T%!E -EF TO lcl_emp&
e> T%!E -EF TO lcl_emp.
*1 T*e ne4t line of code B*as een commentedC results in runtime error as t*e
*1 o5ect is not 3et created.
*e.-6s*o:_detailsB C.
C-EATE O#0ECT e..
C-EATE O#0ECT e/.
e.-6set_detailsB ED!O-TINA im_eno 7 .8.
im_ename 7 9A5a39
im_dno 7 /8 C.
e/-6set_detailsB ED!O-TINA im_eno 7 .8/
im_ename 7 9Ei5a39
im_dno 7 FG C.
e.-6s*o:_detailsB C.
e/-6s*o:_detailsB C.

sHip.

*1 T*ou'* t*e set_details met*od doesn9t allo: us to assi'n a :ron' dept numer&
*1 :e can still assi'n a :ron' +alue to t*e data memer 9dno9 as t*e data components
*1 are declared in 9!"#LIC SECTION9& and *ence are accessile outside t*e class
e/-6dno 7 FG.
e/-6s*o:_detailsB C.
sHip.

*1 T*e line of code elo: :ill result in t*e reference +ariale e> point to t*e same
*1 o5ect t*at t*e reference +ariale e. is pointin' to. No ne: o5ect is created
*1 for e>.
e> 7 e..
e>-6s*o:_detailsB C.
(n the abo"e progra! though care has been taken in the set_details !ethod that no wrong depart!ent
nu!ber should be assigned to the attribute dno, we could still "iolate this by directly accessing the
attribute and assigning a wrong "alue to it. To a"oid such scenarios, we will ha"e to declare the data
co!ponents in the $R(,AT) 5)&T(%=. The co!ponents of a class declared in pri"ate section are like a
pri"ate property of that class and hence are accessible only with that class but not outside. (n contrast to
the pri"ate section, the co!ponents declared in public section are like a public property and hence are
accessible e"erywhere, i.e., within the class and also outside the class.
-E!O-T ?OO!_!"#LIC_!-IEATE.
*---------------------------------------------------------------
* CLASS lcl_emp DEFINITION
*---------------------------------------------------------------
CLASS lcl_emp DEFINITION.
!"#LIC SECTION.
)ET@ODS$ set_details I)!O-TINA im_eno T%!E i
im_ename T%!E strin'
im_dno T%!E i&
s*o:_details.
!-IEATE SECTION.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*---------------------------------------------------------------
* CLASS lcl_emp I)!LE)ENTATION
*---------------------------------------------------------------
CLASS lcl_emp I)!LE)ENTATION.
)ET@OD set_details.
eno 7 im_eno.
ename 7 im_ename.
IF im_dno 7 .8 O- im_dno 7 /8.
dno 7 im_dno.
ENDIF.
END)ET@OD. (set_details
)ET@OD s*o:_details.
<-ITE$ => 9ENo$9& eno&
9EName$9& ename&
9DNo$9& dno.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_emp I)!LE)ENTATION
************************************************
STA-T-OF-SELECTION.
DATA$ e. T%!E -EF TO lcl_emp&
e/ T%!E -EF TO lcl_emp.
C-EATE O#0ECT$ e.& e/.
e.-6set_detailsB ED!O-TINA im_eno 7 .8.
im_ename 7 9A5a39
im_dno 7 /8 C.
e/-6set_detailsB ED!O-TINA im_eno 7 .8/
im_ename 7 9Ei5a39
im_dno 7 FG C.
e.-6s*o:_detailsB C.
e/-6s*o:_detailsB C.
*1 An3 attempt to access t*e pri+ate components of a class outside t*e class *1 :ill result
in s3nta4 Bcompile timeC error.
*e/-6dno 7 FG.
C,(ST-.CT,-: &onstructor is a special !ethod that gets i!plicitly called i!!ediately after an ob>ect is
created using the &R)AT) %#9)&T co!!and. There is no need to e2plicitly call the constructor
!ethod? in fact, the constructor !ethod cannot be called e2plicitly. The na!e of this !ethod has to be
@&%=5TRA&T%RB.
The &R)AT) %#9)&T co!!and perfor!s * tasks:
.. Allocates the !e!ory reCuired for an ob>ects attributes (+ (nstance attributes only .
/. &alls the !ethod with the na!e @constructorB of the corresponding class.
*. Returns the reference of the ob>ect to the associated reference "ariable.
-E!O-T ?OO!_CONST-"CTO-.
*----------------------------------------------------------------------*
* CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
!"#LIC SECTION.
*1 It is 'ood codin' con+ention to pre,4 import parameters of a constructor
*1 :it* a pre,4 imc_
)ET@ODS$ constructor I)!O-TINA imc_eno T%!E i
imc_ename T%!E strin'
imc_dno T%!E i&
s*o:_details.
!-IEATE SECTION.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp I)!LE)ENTATION.
)ET@OD constructor.
eno 7 imc_eno.
ename 7 imc_ename.
dno 7 imc_dno.
END)ET@OD. (constructor
)ET@OD s*o:_details.
<-ITE$ => 9ENo$9& eno&
9EName$9& ename&
9DNo$9& dno.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_emp I)!LE)ENTATION
************************************************
STA-T-OF-SELECTION.
DATA$ e. T%!E -EF TO lcl_emp&
e/ T%!E -EF TO lcl_emp&
e> T%!E -EF TO lcl_emp.

C-EATE O#0ECT e. ED!O-TINA imc_eno 7 .8.
imc_ename 7 9A5a39
imc_dno 7 /8.
C-EATE O#0ECT e/ ED!O-TINA imc_eno 7 .8/
imc_ename 7 9Ei5a39
imc_dno 7 .8.
e.-6s*o:_detailsB C.
e/-6s*o:_detailsB C.
e> 7 e..
e>-6s*o:_detailsB C.
In'e!itance:
(nheritance is a relationship in which one class (the sub class inherits all the !ain characteristics of
another class (the 5uper class.
The sub class can also add new co!ponents (attributes, !ethods and so on and replace or e2tend
inherited !ethods with its own i!ple!entations. The addition (=7)R(T(=D ER%0 is used in the class
definition to indicate fro! which other class the current class is inheriting fro!.
-E!O-T ?OO!_IN@E-ITANCE.
*----------------------------------------------------------------------*
* CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
!"#LIC SECTION.
)ET@ODS$ set_details I)!O-TINA im_eno T%!E i
im_ename T%!E strin'
im_dno T%!E i&
s*o:_details.
!-IEATE SECTION.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp I)!LE)ENTATION.
)ET@OD set_details.
eno 7 im_eno.
ename 7 im_ename.
IF im_dno 7 .8 O- im_dno 7 /8.
dno 7 im_dno.
ENDIF.
END)ET@OD. (set_details
)ET@OD s*o:_details.
<-ITE$ => 9ENo$9& eno&
9EName$9& ename&
9DNo$9& dno.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
* CLASS lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp DEFINITION IN@E-ITINA F-O) lcl_emp.
!"#LIC SECTION.
)ET@ODS$ set_*_:a'e I)!O-TINA im_*_:a'e T%!E i&
s*o:_*_:a'e.
!-IEATE SECTION.
DATA$ *_:a'e T%!E i.
ENDCLASS. (lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_contract_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp I)!LE)ENTATION.
)ET@OD set_*_:a'e.
*_:a'e 7 im_*_:a'e.
END)ET@OD. (set_*_:a'e
)ET@OD s*o:_*_:a'e.
<-ITE$ 9@ <a'e$9& *_:a'e.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_contract_emp I)!LE)ENTATION
**************************
STA-T-OF-SELECTION.
DATA$ ce T%!E -EF TO lcl_contract_emp.
C-EATE O#0ECT$ ce.
ce-6set_detailsB ED!O-TINA im_eno 7 .8F
im_ename 7 9-a59
im_dno 7 .8 C.
ce-6set_*_:a'eB ED!O-TINA im_*_:a'e 7 /88 C.
ce-6s*o:_detailsB C.
ce-6s*o:_*_:a'eB C.
(n the abo"e progra! to display the details of a &ontract )!ployee ob>ect we ha"e to in"oke two
!ethods. The show_details !ethod inherited fro! the super class 4&48)0$, and the show_h_wage
!ethod that has been newly defined in the subclass 4&48&%=TRA&T8)0$. This is so because the
i!ple!entation of the show_details !ethod as pro"ided 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 infor!ation
we had to add a new !ethod show_h_wage in the subclass. To a"oid coding a new !ethod, the
show_details !ethod which has been inherited in the subclass, fro! the super class, has to be redefined
so as to display the "alues in the attributes of both the superclass and subclass.
%et'o" ,$e!!i"in*: Redefining a !ethod in the subclass, which has been inherited fro! its superclass,
is called !ethod o"erriding. While redefining an inherited !ethod the functionality defined in the
superclass can be retained as is and so!e !ore additional functionality can be pro"ided, or the inherited
functionality can be nullified and altogether a new functionality can be pro"ided.
=%T):
. While o"erriding a !ethod in the subclass, the signature of the !ethod has to e2actly !atch with
the signature of the corresponding !ethod in the superclass.
/ (n the subclass !ethod, during redefinition (o"erriding, if we ha"e to access the i!ple!entation
of the corresponding !ethod in the superclass we will ha"e to use the keyword 5A$)R. The
keyword 5A$)R is a reference (pseudo reference to the current class superclass
i!ple!entation.
* While o"erriding a !ethod if the keyword 5A$)R is not used, it !eans that the !ethods
functionality as i!ple!ented in the superclass is being nullified in the subclass and an altogether
new functionality is being pro"ided.
%et'o" Si*nat!e: The signature of a !ethod includes the nu!ber of para!eters, type of para!eters
(i!porting, e2porting and-or changing? and the data types, and the seCuence of para!eters.
-E!O-T ?OO!_OEE--IDINA.
*----------------------------------------------------------------------*
* CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
!"#LIC SECTION.
)ET@ODS$ set_details I)!O-TINA im_eno T%!E i
im_ename T%!E strin'
im_dno T%!E i&
s*o:_details.
!-IEATE SECTION.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp I)!LE)ENTATION.
)ET@OD set_details.
eno 7 im_eno.
ename 7 im_ename.
IF im_dno 7 .8 O- im_dno 7 /8.
dno 7 im_dno.
ENDIF.
END)ET@OD. (set_details
)ET@OD s*o:_details.
<-ITE$ => 9ENo$9& eno&
9EName$9& ename&
9DNo$9& dno.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
* CLASS lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp DEFINITION IN@E-ITINA F-O) lcl_emp.
!"#LIC SECTION.
)ET@ODS$ set_*_:a'e I)!O-TINA im_*_:a'e T%!E i&
s*o:_details -EDEFINITION.
!-IEATE SECTION.
DATA$ *_:a'e T%!E i.
ENDCLASS. (lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_contract_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp I)!LE)ENTATION.
)ET@OD set_*_:a'e.
*_:a'e 7 im_*_:a'e.
END)ET@OD. (set_*_:a'e
)ET@OD s*o:_details.
super-6s*o:_detailsB C. 2retainin' t*e implementation pro+ided in superclass
<-ITE$ 9@ <a'e$9& *_:a'e. 2additional functionalit3 ein' add in suclass
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_contract_emp I)!LE)ENTATION
**************************
STA-T-OF-SELECTION.
DATA$ ce T%!E -EF TO lcl_contract_emp.
C-EATE O#0ECT$ ce.
ce-6set_detailsB ED!O-TINA im_eno 7 .8F
im_ename 7 9-a59
im_dno 7 .8 C.
ce-6set_*_:a'eB ED!O-TINA im_*_:a'e 7 /88 C.
ce-6s*o:_detailsB C.
(n the abo"e progra! by o"erriding the show_details !ethod in the 4&48&%=TRA&T8)0$ class, we
e2tended the inherited functionality of that !ethod. #ut, if we !ake a si!ilar atte!pt to o"er the
set_details !ethod, it will not be possible. This is because in the subclass 4&48&%=TRA&T8)0$ the
set_details !ethod will ha"e to accept 1 para!eters (including h_wage, where as the set_details
!ethod in the superclass 4&48)0$ accepts only * para!eters. #y adding the 1
th
para!eter the !ethod
signature will change, and this is not allowed while o"erriding a !ethod. This drawback can be o"erco!e
by using a constructor instead of the set_details !ethod, as the signature of a constructor in a subclass
need not necessarily !atch with the constructor of its superclass.
-E!O-T ?OO!_S"#CLASS_AND_CONST-"CTO-.
*----------------------------------------------------------------------*
* CLASS lcl_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_emp DEFINITION.
!"#LIC SECTION.
)ET@ODS$ constructor I)!O-TINA imc_eno T%!E i
imc_ename T%!E strin'
imc_dno T%!E i&
s*o:_details.
!-IEATE SECTION.
DATA$ eno T%!E i&
ename T%!E strin'&
dno T%!E i.
ENDCLASS. (lcl_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_emp I)!LE)ENTATION.
)ET@OD constructor.
eno 7 imc_eno.
ename 7 imc_ename.
IF imc_dno 7 .8 O- imc_dno 7 /8.
dno 7 imc_dno.
ENDIF.
END)ET@OD. (constructor
)ET@OD s*o:_details.
<-ITE$ => 9ENo$9& eno&
9EName$9& ename&
9DNo$9& dno.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
* CLASS lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp DEFINITION IN@E-ITINA F-O) lcl_emp.
!"#LIC SECTION.
)ET@ODS$ constructor I)!O-TINA imc_eno T%!E i
imc_ename T%!E strin'
imc_dno T%!E i
imc_*_:a'e T%!E i&
s*o:_details -EDEFINITION&
'et_total_:a'e importin' im_*rs_:orHed t3pe i
e4portin' e4_t_:a'e t3pe i.
!-IEATE SECTION.
DATA$ *_:a'e T%!E i.
ENDCLASS. (lcl_contract_emp DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_contract_emp I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_contract_emp I)!LE)ENTATION.
)ET@OD constructor.
super-6constructorB ED!O-TINA imc_eno 7 imc_eno
imc_ename 7 imc_ename
imc_dno 7 imc_dno C.
*_:a'e 7 imc_*_:a'e.
END)ET@OD. (constructor
)ET@OD s*o:_details.
super-6s*o:_detailsB C.
<-ITE$ 9@ <a'e$9& *_:a'e.
END)ET@OD. (s*o:_details

)ET@OD 'et_total_:a'e.
e4_t_:a'e 7 im_*rs_:orHed * *_:a'e.
END)ET@OD.
ENDCLASS. (lcl_contract_emp I)!LE)ENTATION
**************************
STA-T-OF-SELECTION.
DATA$ ce T%!E -EF TO lcl_contract_emp&
t_:a'e t3pe i.

C-EATE O#0ECT$ ce ED!O-TINA imc_eno 7 .8F
imc_ename 7 9-a59
imc_dno 7 .8
imc_*_:a'e 7 /88.
ce-6s*o:_detailsB C.
ce-6'et_total_:a'eB ED!O-TINA im_*rs_:orHed 7 >I
I)!O-TINA e4_t_:a'e 7 t_:a'e C.
:rite$ => 9Total :a'e$9& t_:a'e.
(otes abot /eywo!" S.P0-:
.. (n the constructor of a subclass it is mandatory to in"oke the superclass constructor in the "ery
first line of the constructors code using the keyword 5A$)R.
/. (n other !ethods the 5A$)R can be used in any line.
*. 5A$)R can be used in only the !ethods that are being redefined. 0oreo"er, say an inherited
!ethod !eth82 is being redefined in a subclass? such !ethod can access only the
i!ple!entation of !eth82 of its superclass using 5A$)R, but not the other !ethod
i!ple!entations fro! its superclass.
1nctional %et'o"s:
0ethods with R)TAR=(=D para!eters are known as functional !ethods (and those without
R)TAR=(=D para!eters are known as non6functional !ethods.
A !ethod defined with R)TAR=(=D para!eters cannot ha"e )F$%RT(=D 3 &7A=D(=D
para!eters.
R)TAR=(=D para!eters are always passed by "alue and not by reference.
Eunctional !ethods can be called in assign!ent operation, and also as part of an e2pression
(logical e2pressions 3 arith!etic e2pressions, which is not possible with non6functional !ethods.
7owe"er, a functional !ethod cannot be called as part of WR(T) state!ent.
-E!O-T Joop_functional.
*----------------------------------------------------------------------*
* CLASS lcl_arit*metic DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_arit*metic DEFINITION.
!"#LIC SECTION.
)ET@ODS$ add I)!O-TINA im_ar'. T%!E i
im_ar'/ T%!E i
-ET"-NINA +alueBsumC T%!E i&
sutract I)!O-TINA im_ar'. T%!E i
im_ar'/ T%!E i
-ET"-NINA +alueBdiKC T%!E i.
ENDCLASS. (lcl_arit*metic DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_arit*metic I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_arit*metic I)!LE)ENTATION.
)ET@OD add.
sum 7 im_ar'. L im_ar'/.
END)ET@OD. (add
)ET@OD sutract.
diK 7 im_ar'. - im_ar'/.
END)ET@OD. (sutract
ENDCLASS. (lcl_arit*metic I)!LE)ENTATION
***************************************
STA-T-OF-SELECTION.
DATA$ +. T%!E i&
+/ T%!E i&
art* T%!E -EF TO lcl_arit*metic.
C-EATE O#0ECT art*.
*1 Callin' a functional met*od in an assi'nment operation
+. 7 art*-6addB im_ar'. 7 >G im_ar'/ 7 .G C.
+/ 7 art*-6sutractB im_ar'. 7 F8 im_ar'/ 7 .G C.
<-ITE$ => 9E.$9& +.&
9E/$9& +/.
*1 Callin' a functional met*od as a part of lo'ical e4pression
IF art*-6addB im_ar'. 7 /G im_ar'/ 7 /G C 6 F8.
<-ITE$ => 9Sum is 'reater t*an F89.
ENDIF.
=%T):
There are two synta2es a"ailable to call a functional !ethod. The abo"e defined functional !ethod add
can be called as below:
+. 7 art*-6addB im_ar'. 7 >G im_ar'/ 7 .G C.
O-
CALL )ET@OD art*-6addB ED!O-TINA im_ar'. 7 >G im_ar'/ 7 .G
-ECEIEINA sum 7 +. C.
The first approach is usually the preferred approach fro! the "iew of code opti!iGation.

%0: The keyword 0) refers to the current ob>ect of the current class.
*----------------------------------------------------------------------*
* CLASS lcl_student DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_student DEFINITION.
!"#LIC SECTION.
)ET@ODS$ constructor I)!O-TINA imc_id T%!E i
imc_name T%!E strin'&
s*o:_details.
!-IEATE SECTION.
DATA$ id T%!E i&
name T%!E strin'.
ENDCLASS. (lcl_student DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_student I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_student I)!LE)ENTATION.
)ET@OD constructor.
id 7 imc_id.
name 7 imc_name.
END)ET@OD. (constructor
)ET@OD s*o:_details.
<-ITE$ => 9ID$9& id&
9Name$9& name.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_student I)!LE)ENTATION
The class lcl8student can also be defined as below:
*----------------------------------------------------------------------*
* CLASS lcl_student DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_student DEFINITION.
!"#LIC SECTION.
)ET@ODS$ constructor I)!O-TINA id T%!E i
name T%!E strin'&
s*o:_details.
!-IEATE SECTION.
DATA$ id T%!E i&
name T%!E strin'.
ENDCLASS. (lcl_student DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_student I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_student I)!LE)ENTATION.
)ET@OD constructor.
)E-6id 7 id.
)E-6name 7 name.
END)ET@OD. (constructor
)ET@OD s*o:_details.
<-ITE$ => 9ID$9& id&
9Name$9& name.
END)ET@OD. (s*o:_details
ENDCLASS. (lcl_student I)!LE)ENTATION
CLASS2DATA 3 CLASS2%0T4,DS
Attributes and !ethods declared &4A556'ATA and &4A5560)T7%'5, respecti"ely, are known as class
co!ponents or static co!ponents, while those declared 'ATA and 0)T7%'5 are known as instance
co!ponents of non6static co!ponents.
=otes about class 3 instance co!ponents:
.. A class !e!ber can be accessed in two ways:
Through the ob>ect reference (only when the reference "ariable points to an ob>ect of the class.
ref_variable25static-member
Through the class na!e (can be accessed before and also after an ob>ect of the class has been
created.
class_name65static_member
/. The i!ple!entation of a class is loaded only once in the !e!ory, which is referred to as Class2
%emo!y? while for each ob>ect a gi"en class that has been created with &R)AT) %#9)&T
co!!and, !e!ory is allocated separately which is referred to as Instance2%emo!y.
*. )ach class attribute occupies space in the class !e!ory and hence there only one copy of each
attribute which is shared between the class i!ple!entation and all the ob>ects of that class.
1. The class i!ple!entation is loaded into the !e!ory only once and that too when the class is
referred to for the first ti!e. Eor this reason the class co!ponents can be accessed e"en before
an ob>ect of that class being created, and this can be done by accessing the class co!ponents
through the class na!e.
class_name65static_member
H. &lass !ethods can access other class co!ponents of the class but cannot access the instance
co!ponents. Which !eans a !ethod defined as &4A5560)T7%' can access only the
attributes that ha"e been defined as &4A556'ATA and the !ethods that ha"e defined as &4A556
0)T7%'5, but not those defined as 'ATA and 0)T7%'5. %n the other hand, instance
!ethods can access both class co!ponents as well as instance co!ponents of the class.
-E!O-T ?OO!_CLASS_INSTANCE_CO)!ONENTS.
*----------------------------------------------------------------------*
* CLASS lcl_student DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_student DEFINITION.
!"#LIC SECTION.
)ET@ODS$ constructor I)!O-TINA imc_id T%!E i
imc_name T%!E strin'&
s*o:_details.
CLASS-)ET@ODS$ 'et_count -ET"-NINA +alueBcntC T%!E i.
!-IEATE SECTION.
DATA$ id T%!E i&
name T%!E strin'.
CLASS-DATA$ count T%!E i. (Count of student o5ects created
ENDCLASS. (lcl_student DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_student I)!LE)ENTATION
*----------------------------------------------------------------------*
CLASS lcl_student I)!LE)ENTATION.
)ET@OD constructor.
ADD . TO count.
id 7 imc_id.
name 7 imc_name.
END)ET@OD. (constructor
)ET@OD s*o:_details.
<-ITE$ => 9ID$9& id&
9Name$9& name.
END)ET@OD. (s*o:_details
)ET@OD 'et_count.
cnt 7 count.
END)ET@OD. ('et_count
ENDCLASS. (lcl_student I)!LE)ENTATION
************************************************
STA-T-OF-SELECTION.
DATA$ stu_count T%!E i.
stu_count 7 lcl_student=>'et_countB C.
<-ITE$ => 9Count efore creatin' an3 o5ects$9& stu_count.
DATA$ s. t3pe ref to lcl_student&
s/ t3pe ref to lcl_student.
C-EATE O#0ECT$ s. e4portin' imc_id 7 .8. imc_name 7 9Ei5a3 Anand9.
stu_count 7 lcl_student=>'et_countB C.
<-ITE$ => 9Count after creatin' ,rst o5ect$9& stu_count.
C-EATE O#0ECT$ s/ e4portin' imc_id 7 .8/ imc_name 7 9S:aroop ;umar9.
stu_count 7 lcl_student=>'et_countB C.
<-ITE$ => 9Count after creatin' second o5ect$9& stu_count.
sHip /.
stu_count 7 s.-6'et_countB C.
<-ITE$ => 9Count +alue t*ru s.-6'et_countB C$9& stu_count.
stu_count 7 s/-6'et_countB C.
<-ITE$ => 9Count +alue t*ru s/-6'et_countB C$9& stu_count.
stu_count 7 lcl_student76'et_countB C.
<-ITE$ => 9Count +alue t*ru lcl_student76'et_countB C$9& stu_count.
De!efe!encin* ob+ects an" Ga!ba*e Collection:
.. DATA$ s. t3pe ref to lcl_student&
/. s/ t3pe ref to lcl_student.
>. C-EATE O#0ECT$ s. e4portin' imc_id 7 .8. imc_name 7 9Ei5a3 Anand9.
F. C-EATE O#0ECT$ s/ e4portin' imc_id 7 .8/ imc_name 7 9S:aroop ;umar9.
G. s. 7 s/.
(n the abo"e piece of code the lines . 3 / declare two reference "ariables (ref6"ar na!es s1 and s2. At
this stage no ob>ects ha"e yet been created. When the line6* gets e2ecuted an ob>ect of class lcl_student
is created with id I .J., and that ob>ect is now being pointed by the ref6"ar s1. 5i!ilarly, when line61 gets
e2ecuted an ob>ect of class lcl_student with id I .J/ gets created the reference of which is held by ref6"ar
s2. #ut, when the line6H gets e2ecuted the reference of the second ob>ect held by s2 get copied into s1.
Therefore, at this stage both the ref6"ars s1 and s2 are pointing the sa!e ob>ect, the one with id I .J/.
What about the first ob>ect with id I .J.K This ob>ect now has no ref6"ar pointing to it, which !eans that
this ob>ect has now been "e2!efe!ence". All the ob>ects which ha"e been de6referenced, i.e, the ob>ects
which are no longer being referred to by any ref6"ar, are !arked for garbage collection. Darbage
&ollector is a ser"ice that is up and running continuously on the 5A$ ser"er that keeps looking for ob>ects
that ha"e been de6referenced (!arked for garbage collection and releases (frees the !e!ory
associated with such ob>ects.
State 3 Be'a$io!:
)"ery ob>ect has a state and beha"iour.
The ter! 5TAT) refers to the "alues held by the data fields of an ob>ect. As class attributes occupy
space in class6!e!ory and not in the ob>ect6!e!ory, they are not considered part of the state of an
ob>ect. (t is the data ("alues held by the instance co!ponents, which occupy space in ob>ect6!e!ory
and are allocated separately for each ob>ect created, that deter!ine the state of an ob>ect.
(f e2actly identical "alues are assigned to the corresponding data fields of two or !ore ob>ects of the
sa!e class, then such ob>ects are said to be in sa!e state.
)2a!ple:
DATA$ s. t3pe ref to lcl_student&
s/ t3pe ref to lcl_student.
C-EATE O#0ECT$ s. e4portin' imc_id 7 .8. imc_name 7 9Ei5a39.
C-EATE O#0ECT$ s/ e4portin' imc_id 7 .8. imc_name 7 9Ei5a39.
(n the abo"e piece of code the ob>ects referred to by the reference "ariables s1 and s2 are two different
ob>ects, but the two ob>ects s1 and s2 are said to ha"e the sa!e 5TAT).
The ter! #)7A,(%AR refers to the operations that can be perfor!ed (processing logic that can be
in"oked through an ob>ect reference. As the processing logic in a class is i!ple!ented in its !ethods, it
is the different !ethods a"ailable in a class that deter!ines the beha"iour of its ob>ects.

You might also like