ABAP Object Oriented Programming Tutorials Examples 1, OOABAP Programming Tutorials with Examples, Welcome to SAP ABAP Interview Questions.
SAP ABAP Object Oriented Programming Tutorials & Examples
Hello ABAP programmers, we hope you are doing good today!! We sapabapiq.com started a new series of posts on SAP OOABAP tutorials with examples to learn and understand the object oriented programming concepts in ABAP. This is the first post in this series of ABAP Object Oriented Programming Tutorials with examples. This is basically for novice ABAP programmers, if you are an experienced consultant just refresh your knowledge.
What is a Class in OOABAP ?
•Classes are the central element of object-orientation.
•A Class is an abstract description of an object.
•Classes are templates for objects.
•The attributes of objects are defined by the components of the class, which describe the state and behavior of objects.
•You define global classes and interfaces in the Class Builder (Transaction SE24) in the ABAP Workbench.
•They are stored centrally in class pools in the class library in the R/3 Repository.
•All of the ABAP programs in an R/3 System can access the global classes.
OOABAP Programming Tutorials with Examples
1) Accessibility of different sections of a class |
|
Theme of the Program
|
From this program, one will
learn:-
The following program will
also show that :-
* Data
declared in public section can be accessed by the class itself, by its
sub classes as well as by other users outside the class.
* Data
declared in the protected section can be accessed by the class itself, and also by its sub classes but not by
external users outside the class.
* Data
declared in the private section can be accessed by the class only, but not by
its sub classes and by external users outside the class.
|
Brief Description of the Program
|
This program contains a class : parent class with
following attributes in different sections:-
Commondata in public section
Protectdata in
private section
Privatedata in
private section
The method showval in class : parentclass
displays values of all the attributes.
This demonstrates that class
can access all its attributes.
Class childclass is a subclass of class parentclass, which has a
method : subval.
It displays the value for the data : commondata and
protectdata .
Then, it changes the values for both and displays them
again.
This demonstrates that
subclass can access/change public/ protected attributes of superclass.
In the START-OF-SELECTION event, object : parent
is instantiated from class : parentclass and object : child is
instantiated from class : childclass.
Then , the method showval of parent(object
of parentclass) and method subval of child(object of
childclass) is called , which displays the values of different attributes.
Then, the public attribute of object parent is changed and
the changed value is displayed.
This demonstrates that
external users can change/ display public attributes of a class.
|
OOABAP Program Code
REPORT YSUBDEL LINE-SIZE 120.
CLASS parentclass DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value
'Accessible to all'.
METHODS : SHOWVAL.
PROTECTED SECTION.
DATA : protectdata(40) type c value
'Protected data'.
private section.
data : privatedata(30) type c value 'Private
data'.
ENDCLASS.
CLASS parentclass IMPLEMENTATION.
METHOD : SHOWVAL.
write:/5 'All data from parentclass
shown:-'.
write:/ sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA,
/5 PRIVATEDATA.
endmethod.
endclass.
CLASS
childclass DEFINITION INHERITING FROM parentclass.
PUBLIC SECTION .
METHODS : subval.
ENDCLASS.
CLASS childclass IMPLEMENTATION.
method : subval.
skip 1.
write:/5 'Data of parent shown from
child-'.
write:/5 sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA.
Commondata = 'Public data changed in
subclass'.
Protectdata = 'Protected data changed in
subclass'.
write:/5 sy-uline.
WRITE:/5 COMMONDATA,
/5 PROTECTDATA.
endmethod.
endclass.
START-OF-SELECTION.
DATA : parent type ref to parentclass ,
child type ref to childclass .
create object : parent ,
child .
call method : parent->showval ,
child->subval.
skip 2.
parent->commondata = ‘User changing public data’.
write:/5 parent->commondata.
|
Output of the ABAP Program
|
All data from
parentclass shown:-
Accessible to
all
Protected data
Private data
Data of parent
shown from child-
Accessible to
all
Protected data
Public data changed
in subclas
Protected data
changed in subclass
User changing
public data
|
2) Subclass cannot access the private component of superclass |
|
Theme of the Program
|
The program
demonstrates that subclasses cannot access the private components of
superclass.
|
OOABAP Program description
|
The program used here
is similar to above with change in the method : subval of class : childclass
. This method is now attempting to access the attribute : privatedata , which
is a private attribute of its
superclass : parentclass.
On compilation, the
program will give a compilation error.
This
demonstrates that private components of superclass cannot be accessed by
subclasses.
|
OOABAP Programming Example
|
Take the first
program. Only change the method : subval of class : childclass
as follows:-
method : subval.
skip 1.
write:/5
'All data from parent class shown by subclass'.
write:/5
sy-uline.
WRITE:/5
COMMONDATA,
/5
PROTECTDATA,
/5
privatedata.
endmethod.
|
Output of the OOABAP Program
|
The program will not
compile. It will show an error message:-
|
3) External users cannot access protected/private components of a class |
|
Theme of the Program
|
This program will demonstrate that external users cannot
access the protected and private components of a class
|
OOABAP Program Description
|
In this program , class C1 has three attributes
declared in different sections as follows:-
* Commondata in
public section
* Protectdata in
private section
* Privatedata
in private section
In the main program, an object , OBJ1 is created
from class C1 and an incorrect
attempt is made to display the protected and private
attribute of class C1 using its object OBJ1.
Compilation of this program produces an error.
This demonstrates : protected and private components of a class
cannot be accessed by external users.
|
Example code of OOABAP Program
|
REPORT YSUBDEL LINE-SIZE 120.
CLASS c1 DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible
to all'.
PROTECTED SECTION.
DATA : protectdata(40) type c value
'Protected data'.
private section.
data : privatedata(30) type c value 'Private
data'.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
DATA : obj1 type ref to c1.
create object : obj1.
write:/5 obj1->protectdata ,
obj1->privatedata.
|
Output of the Program
|
On compilation, an error will be generated which will
prove that protected and private components of a class cannot be accessed by
external users.
|
Read continuation of SAP Object Oriented Programming Tutorials with Examples in next posts.
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.