Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

****************************My First program*******************************

REPORT ZDEMO_OBJECT.
CLASS Class1 Definition.
Public Section.
DATA: text1(45) VALUE 'ABAP Objects.'.
METHODS: Display1.
ENDCLASS.

CLASS Class1 Implementation.


METHOD Display1.
Write:/ 'This is the Display method.'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA: Class1 TYPE REF TO Class1.
CREATE Object: Class1.
Write:/ Class1->text1.
CALL METHOD: Class1->Display1.

===================================================================================
===
*****************************Section Access********************************

Report ZAccess1.
CLASS class1 Definition.
PUBLIC Section.
Data: text1 Type char25 Value 'Public Data'.
Methods meth1.

PROTECTED Section.
Data: text2 Type char25 Value 'Protected Data'.

PRIVATE Section.
Data: text3 Type char25 Value 'Private Data'.
ENDCLASS.

CLASS class1 Implementation.


Method meth1.
Write: / 'Public Method:',
/ text1,
/ text2,
/ text3.
Skip.
EndMethod.
ENDCLASS.

Start-Of-Selection.
Data: Objectx Type Ref To class1.
Create Object: Objectx.
CALL Method: Objectx→meth1.
Write: / Objectx→text1.

===================================================================================
*****************************Static Class*****************************

Report ZStatic1.
CLASS class1 Definition.
PUBLIC Section.
CLASS-DATA: name1 Type char45,
data1 Type I.
Methods: meth1.
ENDCLASS.

CLASS class1 Implementation.


Method meth1.
Do 4 Times.
data1 = 1 + data1.
Write: / data1, name1.
EndDo.
Skip.
EndMethod.
ENDCLASS.

Start-Of-Selection.
class1⇒name1 = 'ABAP Object Oriented Programming'.
class1⇒data1 = 0.
Data: Object1 Type Ref To class1,
Object2 Type Ref To class1.

Create Object: Object1, Object2.


CALL Method: Object1→meth1,
Object2→meth1.
===================================================================================
=================
********************Constructor*****************

Report ZConstructor1.
CLASS class1 Definition.
PUBLIC Section.
Methods: method1, constructor.
ENDCLASS.

CLASS class1 Implementation.


Method method1.
Write: / 'This is Method1'.
EndMethod.

Method constructor.
Write: / 'Constructor Triggered'.
EndMethod.
ENDCLASS.

Start-Of-Selection.
Data Object1 Type Ref To class1.
Create Object Object1.
===================================================================================
=====================
*********************ME Operator***********************************
Report ZMEOperator1.
CLASS class1 Definition.
PUBLIC Section.

Data text1 Type char25 Value 'This is CLASS Attribute'.


Methods method1.
ENDCLASS.

CLASS class1 Implementation.


Method method1.

Data text1 Type char25 Value 'This is METHOD Attribute'.


Write: / ME→text1,
/ text1.
ENDMethod.
ENDCLASS.

Start-Of-Selection.
Data objectx Type Ref To class1.
Create Object objectx.
CALL Method objectx→method1.
===================================================================================
===========================
*****************************Inheretence*******************************************
Report ZINHERITAN_1.
CLASS Parent Definition.
PUBLIC Section.
Data: w_public(25) Value 'This is public data'.
Methods: ParentM.
ENDCLASS.

CLASS Child Definition Inheriting From Parent.


PUBLIC Section.
Methods: ChildM.
ENDCLASS.

CLASS Parent Implementation.


Method ParentM.
Write /: w_public.
EndMethod. ENDCLASS.

CLASS Child Implementation.


Method ChildM.
Skip.
Write /: 'Method in child class', w_public.
EndMethod.
ENDCLASS.

Start-of-selection.
Data: Parent Type Ref To Parent,
Child Type Ref To Child.
Create Object: Parent, Child.
Call Method: Parent→ParentM,
child→ChildM.
======================================================================
Access Public Protected Private
Same calss Yes Yes Yes
Derived class Yes Yes No
Outside class Yes No No
==============================================================================

You might also like