Simple Local Class
Introduction:
- As a beginner, let us consider a short & simple example of a program using Object Oriented Concept.
- We will simply cover the following basic concepts:
- Define & Implement Class C.
- Create Visibility for the attributes of Class C.
- Define & Implement Method M.
- Create an Object for Class C.
- Call a Method M for Class C.
Steps:
- Create an executable program in SE 38.
- Program: ZOABAP_SIMPLEPROG.
- Here we will create a Visibility for the attributes of the class as PUBLIC SECTION.
- For parameters PART1, PART2, FULL.
- For Method M.
- Class Attribute Visibility
- Public:
- All the components which are assigned to this section are public and can be accessed outside the class, methods of the subclass, and the methods of the class itself.
- They form the external interface of the class.
- Protected:
- The components of this section are protected and can be accessed by the methods of subclass and the methods of the class itself.
- They form the interface of the class and its subclass.
- Private:
- Components of this section are private and can only be used in the methods of the class itself.
- The private components are not part of the external interface of the class.
- Here we will create Implementation for Method M.
- We will create a simple scenario for 'CONCATENATE' and 'WRITE' as shown above.
- To call our Method M we have to create an Object for reference parameter OBJ of Class C.
- Here method M is an 'INSTANT' Method.
- Class Attributes
- Instance Attributes:
- You declare them using the DATA statement.
- To access we need to create an Object.
- Instance attributes are accessed using the Object Component Selector ‘->’.
- Static Attributes:
- You declare them using the CLASS-DATA statement.
- To access we need not create an Object.
- Static attributes are accessed using the Class Component Selector ‘=>’.
5. Now save, activate and execute the program.
Example Code:
REPORT ZOABAP_SIMPLEPROG.
CLASS C DEFINITION.
PUBLIC SECTION.
DATA: PART1(5) TYPE C,
PART2(5) TYPE C,
FULL TYPE STRING.
METHODS: M.
ENDCLASS.
CLASS C IMPLEMENTATION.
METHOD M.
PART1 = 'VIDYA'.
PART2 = 'SAGAR'.
WRITE: PART1 COLOR 3, /
PART2 COLOR 4.
CONCATENATE PART1 '' PART2 '' INTO FULL.
WRITE:/ full COLOR 5.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: OBJ TYPE REF TO C.
CREATE OBJECT OBJ.
CALL METHOD:
OBJ->M.
Note:
- If you like what you see please Follow and Share.
- For ABAP Workflow-related content, follow my blog ABAP Workflow For Beginners.
Comments
Post a Comment