This document provides tutorials and examples on object-oriented programming concepts in SAP ABAP. It begins with an introduction to classes in OOABAP, defining classes as templates for objects. It then provides 3 examples to demonstrate different aspects of class visibility and accessibility in ABAP OO:
1) How public, protected, and private sections of a class control accessibility of attributes and methods to the class itself, subclasses, and external users.
2) How private attributes of a superclass cannot be accessed by subclasses.
3) How protected and private attributes of a class cannot be accessed by external users.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
171 views
SAP ABAP Object Oriented Programming Tutorials
This document provides tutorials and examples on object-oriented programming concepts in SAP ABAP. It begins with an introduction to classes in OOABAP, defining classes as templates for objects. It then provides 3 examples to demonstrate different aspects of class visibility and accessibility in ABAP OO:
1) How public, protected, and private sections of a class control accessibility of attributes and methods to the class itself, subclasses, and external users.
2) How private attributes of a superclass cannot be accessed by subclasses.
3) How protected and private attributes of a class cannot be accessed by external users.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7
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 ABAP tutorials with e!amples to learn and understand the ob"ect oriented programming concepts in ABAP. #his is the first post in this series of ABAP b"ect riented Programming #utorials with e!amples. #his is basically for no$ice ABAP programmers, if you are an e!perienced consultant "ust refresh your %nowledge. What is a Class in OOABAP ? &'lasses are the central element of ob"ect(orientation. &A 'lass is an abstract description of an ob"ect. &'lasses are templates for ob"ects. &#he attributes of ob"ects are defined by the components of the class, which describe the state and beha$ior of ob"ects. &)ou define global classes and interfaces in the 'lass Builder *#ransaction S+,-. in the ABAP Wor%bench. &#hey are stored centrally in class pools in the class library in the /01 /epository. &All of the ABAP programs in an /01 System can access the global classes. OOABAP Programming Tutorials ith Examples 1) Accessibility of different sections of a class #heme of the Program 2rom this program, one will learn3( 4. How to define, implement and instantiate a class. ,. What are the different sections of $isibility in a class. 1. How to define instance attributes and get them accessed by e!ternal users. #he following program will also show that 3( 5 6ata declared in public section can be accessed by the class itself, by its sub classes as well as by other users outside the class. 5 6ata declared in the protected section can be accessed by the class itself, and also by its sub classes but not by e!ternal users outside the class. 5 6ata declared in the pri$ate section can be accessed by the class only, but not by its sub classes and by e!ternal users outside the class. Brief 6escription of the Program #his program contains a class 3 parent class with following attributes in different sections3( Commondata in public section Protectdata in pri$ate section Privatedata in pri$ate section #he method showval in class 3 parentclass displays $alues of all the attributes. This demonstrates that class can access all its attributes. 'lass childclass is a subclass of class parentclass, which has a method 3 subval. 7t displays the $alue for the data 3 commondata and protectdata . #hen, it changes the $alues for both and displays them again. This demonstrates that subclass can access/change public/ protected attributes of superclass. 7n the STAT!"#!S$%$CT&"' e$ent, ob"ect 3 parent is instantiated from class 3 parentclass and ob"ect 3 child is instantiated from class 3 childclass. #hen , the method showval of parent*ob"ect of parentclass. and method subval of child*ob"ect of childclass. is called , which displays the $alues of different attributes. #hen, the public attribute of ob"ect parent is changed and the changed $alue is displayed. This demonstrates that e(ternal users can change/ display public attributes of a class. ABAP Program 'ode /+P/# )S8B6+9 97:+(S7;+ 4,<. '9ASS parentclass 6+27:7#7: . P8B97' S+'#7:. 6A#A 3 commondata*1<. type c $alue =Accessible to all=. >+#H6S 3 SHW?A9. P/#+'#+6 S+'#7:. 6A#A 3 protectdata*-<. type c $alue =Protected data=. pri$ate section. data 3 pri$atedata*1<. type c $alue =Pri$ate data=. +:6'9ASS. '9ASS parentclass 7>P9+>+:#A#7:. >+#H6 3 SHW?A9. write30@ =All data from parentclass shown3(=. write30 sy(uline. W/7#+30@ '>>:6A#A, 0@ P/#+'#6A#A, 0@ P/7?A#+6A#A. endmethod. endclass. '9ASS childclass 6+27:7#7: 7:H+/7#7:A 2/> parentclass. P8B97' S+'#7: . >+#H6S 3 sub$al. +:6'9ASS. '9ASS childclass 7>P9+>+:#A#7:. method 3 sub$al. s%ip 4. write30@ =6ata of parent shown from child(=. write30@ sy(uline. W/7#+30@ '>>:6A#A, 0@ P/#+'#6A#A. 'ommondata B =Public data changed in subclass=. Protectdata B =Protected data changed in subclass=. write30@ sy(uline. W/7#+30@ '>>:6A#A, 0@ P/#+'#6A#A. endmethod. endclass. S#A/#(2(S+9+'#7:. 6A#A 3 parent type ref to parentclass , child type ref to childclass . create ob"ect 3 parent , child . call method 3 parent(Cshow$al , child(Csub$al. s%ip ,. parent(Ccommondata B D8ser changing public dataE. write30@ parent(Ccommondata. utput of the ABAP Program
All data from parentclass shown3(
Accessible to all Protected data Pri$ate data
6ata of parent shown from child(
Accessible to all Protected data
Public data changed in subclas Protected data changed in subclass
8ser changing public data
)) Subclass cannot access the private component of superclass #heme of the Program #he program demonstrates that subclasses cannot access the pri$ate components of superclass. ABAP Program description #he program used here is similar to abo$e with change in the method 3 sub$al of class 3 childclass . #his method is now attempting to access the attribute 3 pri$atedata , which is a pri$ate attribute of its superclass 3 parentclass. n compilation, the program will gi$e a compilation error. This demonstrates that private components of superclass cannot be accessed by subclasses. #a%e the first program. nly change the method 3 subval ABAP Programming +!ample of class 3 childclass as follows3( method 3 sub$al. s%ip 4. write30@ =All data from parent class shown by subclass=. write30@ sy(uline. W/7#+30@ '>>:6A#A, 0@ P/#+'#6A#A, 0@ pri$atedata. endmethod. utput of the ABAP Program #he program will not compile. 7t will show an error message3( *) $(ternal users cannot access protected/private components of a class #heme of the Program #his program will demonstrate that e!ternal users cannot access the protected and pri$ate components of a class ABAP Program 6escription 7n this program , class C1 has three attributes declared in different sections as follows3( 5 Commondata in public section 5 Protectdata in pri$ate section 5 Privatedata in pri$ate section 7n the main program, an ob"ect , "+,1 is created from class C1 and an incorrect attempt is made to display the protected and pri$ate attribute of class C1 using its ob"ect "+,1. 'ompilation of this program produces an error. This demonstrates - protected and private components of a class cannot be accessed by e(ternal users. +!ample code of ABAP Program /+P/# )S8B6+9 97:+(S7;+ 4,<. '9ASS c4 6+27:7#7: . P8B97' S+'#7:. 6A#A 3 commondata*1<. type c $alue =Accessible to all=. P/#+'#+6 S+'#7:. 6A#A 3 protectdata*-<. type c $alue =Protected data=. pri$ate section. data 3 pri$atedata*1<. type c $alue =Pri$ate data=. +:6'9ASS. '9ASS c4 7>P9+>+:#A#7:. endclass. S#A/#(2(S+9+'#7:. 6A#A 3 ob"4 type ref to c4. create ob"ect 3 ob"4. write30@ ob"4(Cprotectdata , ob"4(Cpri$atedata.
utput of the Program n compilation, an error will be generated which will pro$e that protected and pri$ate components of a class cannot be accessed by e!ternal users. OOABAP Interview Questions and Answers