‎2008 Jan 31 11:23 AM
Hello! I am beginner of Objects.
I've just started SE24 and what is:
friends? Interfaces? Events? etc....
Where can I read about? Pls, docus, tutorials...
As soon as I create my first class, how can I call up (=implement) it in my REPORT?
Thanks!
‎2008 Jan 31 3:18 PM
Hi Andrei,
Classes and Interfaces
This section describes the definition of classes and interfaces and of their components. Classes and interfaces form the basis for ABAP Objects, the object-oriented part of the ABAP language. Classes and interfaces can be defined in ABAP programs of the following program types:
In a class pool, you use the Class Builder tool of the ABAP Workbench to define exactly one global class of the class library, which can then be used in all other ABAP programs. In the global declaration section of a class pool, you can individually define local data types, classes and interfaces to tbe used in the class pool and make type groups known. Apart from the TYPES and TYPE-POOLS statements, in class pools no other statements are allowed outside of classes and interfaces.
In an interface pool, you use the Class Builder tool of the ABAP Workbench to define exactly one global interface of the class library to be used in all other ABAP programs. In the global declaration section of an interface pool, you are not allowed to define local data types, classes and interfaces. You can declare type groups. Apart from the statement TYPE-POOLS, in interface pools no other statements are allowed outside of the global interfaces.
In all other ABAP programs, except type groups, you can define local classes and interfaces to be used in the program itself.
Apart from the TYPES and TYPE-POOLS statements, in class and interface pools no other statements are allowed outside of CLASS - ENDCLASS or INTERFACE - ENDINTERFACE.
An example of interface program:
TABLES ZVIJIRANK.
DATA IT LIKE TABLE OF ZVIJIRANK WITH HEADER LINE.
INTERFACE I1.
METHODS : GETDATA.
ENDINTERFACE.
CLASS CL1 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
CLASS CL2 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
CLASS CL1 IMPLEMENTATION.
METHOD I1~GETDATA.
SELECT * FROM ZVIJIRANK INTO TABLE IT.
ENDMETHOD.
ENDCLASS.
CLASS CL2 IMPLEMENTATION.
METHOD I1~GETDATA.
SELECT * FROM ZVIJIRANK INTO TABLE IT WHERE REG_NO = '101'.
ENDMETHOD.
ENDCLASS.
DATA: OBJ1 TYPE REF TO CL1,
OBJ2 TYPE REF TO CL2.
START-OF-SELECTION.
CREATE OBJECT: OBJ1 , OBJ2.
CALL METHOD OBJ1->I1~GETDATA.
LOOP AT IT.
WRITE : / IT-REG_NO , IT-NAME , IT-BRANCH.
ENDLOOP.
CALL METHOD OBJ2->I1~GETDATA.
LOOP AT IT.
WRITE : / IT-REG_NO , IT-NAME , IT-BRANCH.
ENDLOOP.
If you have doubt means go through the following links.
http://help.sap.com/saphelp_nw04/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm
hope this is helpful.
cheers,
Hema.
‎2008 Jan 31 11:44 AM
Hi
Classes
It can be regarded as instruction for building an Object
The type of an object is known as its Class
Abstract description of an object
Classes contain components which describe state and behavior of an object.
Global classes can be defined using SE24(Class Builder) and local classes using SE38
Note : Instance of a Class is called Object
Complete definition includes :
Declaration part
Implementation part (If required)
Class MyClass Definition.
Declaration Part ( For attributes, Methods
EndClass. and Events)
If Declaration part contains Methods, then these methods must be implemented in the Implementation part using statement
Class MyClass Implementation.
EndClass
The Components of Class can be :
Attributes
Methods
Events
The declaration part can be divided into three sections namely
PUBLIC SECTION, PROTECTED SECTION and PRIVATE SECTION
which controls the visibility.
Each component of class must be explicitly assigned to one of these three
visibility sections
Components Types
Instance Components :
Instance Dependent
Exist for each Object
Static Components :
Instance Independent
Exist only once for each class
They are retained for the entire Runtime
All objects of a class can access the static attributes of the class
When a static attribute is changed, this change is reflected in all other objects of the class
Attributes
Attributes are data fields in the Class
Can have any ABAP datatype
The contents of the Attribute determine the status of the object.
Can be Instance( DATA Statement) or
Static ( CLASS-DATA Statement)
Note : Within the Class, the addition LIKE can only be used for reference to other attributes of the class. For references to data types from the ABAP Dictionary, only the addition TYPE can be used
Methods
Internal procedures in the Class
Determine behavior of an Object
Can access all attributes of the Class and can thus change the status of an Object
Similar to other ABAP procedures (Subroutines and Function Modules)
Can be Instance ( METHODS) or Static (CLASS-METHODS)
They are declared in the Declaration part and implemented in the Implementation part using statement
Method MyMethod.
.
EndMethod.
Calling a Method
The additions to the above statement are
1. ... EXPORTING p1 = f1 ... pn = fn ( By Value/ By Reference)
2. ... IMPORTING p1 = f1 ... pn = fn ( By Value/ By Reference)
3. ... CHANGING p1 = f1 ... pn = fn ( By Value/ By Reference)
4. ... RETURNING p = f ( By Value)
5. ... EXCEPTIONS except1 = rc1 ... exceptn = rcn
6. ... PARAMETER-TABLE itab
7. ... EXCEPTION-TABLE itab
Variant 2 : CALL METHOD meth( f ).
If it has only one input (IMPORTING) parameter and no other interface
parameters. Here f is actual parameter.
Variant 3 : CALL METHOD meth( p1 = f1 ... pn = fn ).
If the method has only input parameters(IMPORTING) parameter and no other
interface parameters. Here f1 . fn are actual parameter.
Events
Events enable objects or classes to trigger Event Handler Methods in other objects or Classes
Events are of two types : Instance and Static
Instance Events
Declared using the statement EVENTS.
Instance events can only be triggered in Instance Methods.
Static Events
Declared using statement CLASS-EVENTS
Static events can be triggered both by Instance Methods and Static Methods
Registers and deregisters Event Handler Methods dynamically at Runtime
SET HANDLER
This statement can be used in three ways
SET HANDLER h1 .hn FOR ref
This form is used for instance events and registers the Event Handler Methods for only one instance (i.e one object )
ref stands for Class Reference Variable or Interface Reference Variable
SET HANDLER h1 ..hn FOR ALL INSTANCES
This form is used for instance events and registers the Event Handler Methods for all Instances
ref stands for Class Reference Variable or Interface Reference Variable
SET HANDLER h1 ..hn.
This form is used for static events.
In case of Class Reference : It registers Event Handler Methods for triggering from class where the event has been declared.
In case of Interface Reference : It registers the event handler methods for all triggering classes that implement the interface intf.
Creating Object
To create objects we have to use the CREATE OBJECT statement.
CREATE OBJECT cref.
where, cref = Class Reference Variable
Can have interface parameters like EXPORTING or EXCEPTIONS.
If the Class has instance constructor (CONSTRUCTOR), the CREATE OBJECT statement calls it after the object has been created completely
If the Class has static constructor (CLASS_CONSTRUCTOR), and it has not yet been executed, the CREATE OBJECT statement calls it before creating the Object
Sample Program:
Prg1:
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA: I_PUB_V1 TYPE I VALUE 10.
PROTECTED SECTION.
DATA: I_PRO_V1 TYPE I VALUE 20.
PRIVATE SECTION.
DATA: I_PRI_V1 TYPE I VALUE 30.
ENDCLASS. "C1 DEFINITION
START-OF-SELECTION.
DATA: OBJ TYPE REF TO C1.
CREATE OBJECT OBJ.
WRITE:/ OBJ->I_PUB_V1.
Prog2:
CLASS C1 DEFINITION.
PUBLIC SECTION.
EVENTS: E1.
METHODS: M1,
M2 FOR EVENT E1 OF C1.
ENDCLASS. "C1 DEFINITION
*----
*
CLASS C1 IMPLEMENTATION
*----
*
*
*----
*
CLASS C1 IMPLEMENTATION.
METHOD M1.
WRITE:/ 'IM M1 RAISING EVENT E1'.
RAISE EVENT E1.
ENDMETHOD. "M1
METHOD M2.
WRITE:/ 'I M M2 EVENT HANDLER METHOD EXECUTED VIA EVENT E1'.
ENDMETHOD. "M2
ENDCLASS. "C1 IMPLEMENTATION
START-OF-SELECTION.
DATA: OBJ TYPE REF TO C1.
CREATE OBJECT OBJ.
SET HANDLER OBJ->M2 FOR OBJ.
CALL METHOD OBJ->M1.
prog3:
EPORT ZLCL_IF .
*----
*
INTERFACE IF1
*----
*
*
*----
*
INTERFACE IF1.
METHODS: M1 IMPORTING P1 TYPE I
P2 TYPE I
EXPORTING P3 TYPE I.
ENDINTERFACE. "IF1
*----
*
CLASS CL_ABS DEFINITION
*----
*
*
*----
*
CLASS CL_ABS DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: M1,
M2 ABSTRACT IMPORTING P1 TYPE I
P2 TYPE I
EXPORTING P3 TYPE I.
ENDCLASS. "CL_ABS DEFINITION
If these helpful rewards points.
Regards
Pratap.M
‎2008 Jan 31 3:18 PM
Hi Andrei,
Classes and Interfaces
This section describes the definition of classes and interfaces and of their components. Classes and interfaces form the basis for ABAP Objects, the object-oriented part of the ABAP language. Classes and interfaces can be defined in ABAP programs of the following program types:
In a class pool, you use the Class Builder tool of the ABAP Workbench to define exactly one global class of the class library, which can then be used in all other ABAP programs. In the global declaration section of a class pool, you can individually define local data types, classes and interfaces to tbe used in the class pool and make type groups known. Apart from the TYPES and TYPE-POOLS statements, in class pools no other statements are allowed outside of classes and interfaces.
In an interface pool, you use the Class Builder tool of the ABAP Workbench to define exactly one global interface of the class library to be used in all other ABAP programs. In the global declaration section of an interface pool, you are not allowed to define local data types, classes and interfaces. You can declare type groups. Apart from the statement TYPE-POOLS, in interface pools no other statements are allowed outside of the global interfaces.
In all other ABAP programs, except type groups, you can define local classes and interfaces to be used in the program itself.
Apart from the TYPES and TYPE-POOLS statements, in class and interface pools no other statements are allowed outside of CLASS - ENDCLASS or INTERFACE - ENDINTERFACE.
An example of interface program:
TABLES ZVIJIRANK.
DATA IT LIKE TABLE OF ZVIJIRANK WITH HEADER LINE.
INTERFACE I1.
METHODS : GETDATA.
ENDINTERFACE.
CLASS CL1 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
CLASS CL2 DEFINITION.
PUBLIC SECTION.
INTERFACES I1.
ENDCLASS.
CLASS CL1 IMPLEMENTATION.
METHOD I1~GETDATA.
SELECT * FROM ZVIJIRANK INTO TABLE IT.
ENDMETHOD.
ENDCLASS.
CLASS CL2 IMPLEMENTATION.
METHOD I1~GETDATA.
SELECT * FROM ZVIJIRANK INTO TABLE IT WHERE REG_NO = '101'.
ENDMETHOD.
ENDCLASS.
DATA: OBJ1 TYPE REF TO CL1,
OBJ2 TYPE REF TO CL2.
START-OF-SELECTION.
CREATE OBJECT: OBJ1 , OBJ2.
CALL METHOD OBJ1->I1~GETDATA.
LOOP AT IT.
WRITE : / IT-REG_NO , IT-NAME , IT-BRANCH.
ENDLOOP.
CALL METHOD OBJ2->I1~GETDATA.
LOOP AT IT.
WRITE : / IT-REG_NO , IT-NAME , IT-BRANCH.
ENDLOOP.
If you have doubt means go through the following links.
http://help.sap.com/saphelp_nw04/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm
hope this is helpful.
cheers,
Hema.