Classes
A class is used to specify the form of an object and it combines data and methods for manipulating that data into one neat package.
Instantiability of a Class
If a class can be instanced is determined by whether the instances of this class are given, and if so, in which visibility area the instances can be generated, or whether it is an abstract class.
Class Definition and Implementation
The following syntax shows how to define a class −
CLASS <class_name> DEFINITION.
..........
ENDCLASS.
A class definition starts with the keyword CLASS followed by the class name, DEFINITION and the class body. The definition of a class can contain various components of the class such as attributes, methods, and events. When we declare a method in the class declaration, the method implementation must be included in the class implementation. The following syntax shows how to implement a class −
CLASS <class_name> IMPLEMENTATION.
...........
ENDCLASS.
Note − Implementation of a class contains the implementation of all its methods. In ABAP Objects, the structure of a class contains components such as attributes, methods, events, types, and constants.
Attributes
Attributes are data fields of a class that has a data type. They are declared in the tab "Attributes" of class declaration. These attributes can be divided into 2 categories: instance and static attributes.
An instance attribute defines the specific state of an object. The states are different for different objects. An instance attribute is declared by using the DATA statement.
Static attributes define a common state of a class that is shared by all the instances of the class. That is, if you change a static attribute in one object of a class, the change is affected to all other objects of the class as well. A static attribute is declared by using the CLASS-DATA statement.
User-defined Types in Class
In source-code based, declare a new type in public section, then that type will be displayed in the Types tab of form-based
Go to methods tab and add a method with name GET_MATERIALS_FOR_DATE-INSTANCE- PUBLIC-Get materials for a date.
We can declare user-defined types under types tab, go to types tab.
Click on Direct Type entry icon, save, it will take you to a editor
Don' change any thing, just replace types TY_MARA . with the below code.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.
Save, go back to methods, put mouse cursor on GET_MATERIAL_FOR_DATE method, select parameters button and add below parameters.
Class Methods
A method is a procedure that represents the behavior of an object in the class.
Method Parameters
The definition of a method can contain parameters, so that you can supply/retrieve the values to these parameters at the call of method.
Parameter type can use a type in a type group (Properties Tab → enter Type Group/Object Type)
Type of parameters can be of Importing, Exporting, Changing and Returning
With Return type, the method will pass value to its parameter and return after its call by using an assignment operator
Parameter type can be declared as a custom type in public section
Type of method parameter and variable/table type passing into the method must be the same name and structure. You can declare a new type in Class with the same name and structure to be compatible with the type passing into Class Method.
It is possible to declare a generic STANDARD TABLE type or ANY TABLE (for example, you pass a dynamic internal table into parameter)
Accessing Attributes and Methods
Class components can be defined in public, private, or protected visibility sections that control how these components could be accessed. Practically, we define data in private section and related methods in public section so that they can be called from outside of the class as shown in the following program.
The attributes and methods declared in Public section in a class can be accessed by that class and any other class, sub-class of the program.
When the attributes and methods are declared in Protected section in a class, those can be accessed by that class and sub-classes (derived classes) only.
When the attributes and methods are declared in Private section in a class, those can be accessed by only that class and not by any other class.
Example
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.
The above code produces the following output −
Public Method:
Public Data
Protected Data
Private Data
Public Data
Events
Go to Events tab and add a event as below.
NO_MATERIAL-INSTANCE-PUBLIC-No material entered
Define a method
Go to Methods tab and create a method as below
NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.
Save, double click on the method and add some code.
WRITE:/ 'NO material entered'.
Save and activate immediately.
Link event and method and convert the method into event-handler method.
Now we have to link the method to event to make the method as event handler.
Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view icon(see below image).
A pop up will open, provide description, select Event Handler for check box, provide our class name as ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4), enter.
You will see an icon(event handler icon), which means the method is event handler method.
Create a triggering method which will raise the event
Event handler method is created, now we have to trigger that event, the vent can be triggered by using below syntax.
RAISE EVENT <EVENT NAME>.
We will trigger the event for the method GET_MATERIAL_DTAILS (we previously created Get material details ), double click on the method GET_MATERIAL_DTAILS and add the below code.
METHOD GET_MATERIAL_DTAILS.
*Select material data from mara table into exporting parameter ex_mara (work area) for a material no im_matnr
IF IM_MATNR IS INITIAL .
RAISE EVENT NO_MATERIAL.
ELSE.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDIF.
ENDMETHOD.
Use set handler and register event handler method to a particular instance in the program
Now we have to register this event in our SE38 program, to register a event handler method we use below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>. "here INSTANCE is object and EVENT HANDLER METHOD handler method created in se24
Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code
REPORT ZSAPN_CLASS_EVENT.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "class decleration
DATA WA_MARA TYPE MARA. "work area to store material data
PARAMETERS P_MATNR TYPE MARA-MATNR. "material input
CREATE OBJECT LO_MATERIAL. "create object for material calsss
SET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL. "register event handler method
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
Now execute the program with out giving any input, the result will be
Assignment for you: Create and implement a event and event handler method for material not found in the above class, if user provided the input but the data is not there in MARA table, raise an event.
Friend Class
There are certain special cases where a class would want access to other classes private attributes and methods, in such a scenario we can make use of the friends concept in classes.
If a class A declares itself as a friend to class B then class B will have access to all the private and protected attributes and methods of class A.
CREATE PROTECTED/PRIVATE Addition
CLASS class DEFINITION CREATE PROTECTED|PRIVATE.
When using the extension CREATE PROTECTED, the class can only be instantiated from the methods of the class itself, the friend class or by the subclasses. This makes for unique instance management.
Using the extension CREATE PRIVATE, the class can only be instantiated from the methods of the class itself and its friend class.
Object
An object is a special kind of variable that has distinct characteristics and behaviors. The characteristics (attributes) of an object are used to describe the state of an object, and behaviors (methods) represent the actions performed by an object.
An object is a pattern or instance of a class. It represents a real-world entity such as a person or a programming entity like variables and constants. For example, accounts and students are examples of real-world entities.
Creation of an Object
The object creation usually includes the following steps −
Creating a reference variable with reference to the class. The syntax for which is −
DATA: <object_name> TYPE REF TO <class_name>.
Creating an object from the reference variable. The syntax for which is −
CREATE Object: <object_name>.
Example
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.
The above code produces the following output −
ABAP Objects.
This is the Display method.
Note:
Look in view TRDIR. As name enter the name of the class followed by an asterisk. So, ZCL_MYCLASS*. You'll get a list with = signs after the class name, and then the last five characters: CCDEF, CCIMP, CCMAC, CI, CMxxx, CO, CP, CT, CU. Where xxx is alphanumeric.
Each of these corresponds to components of the class. So ...CI is the private section. The CM are the method implementations etc.
You can look at these directly in SE38 to find out which bit relates to which other bit.
But it's not quite the same as the way function groups are done. As far as I can tell, there's no "overall program" which contains these as includes. I do stand to be corrected of course.
Operators
Downcasting ?=
You can declare an object with generic type “Object”
Then in the code to assign the object, we must use the operator ?= to downcast the generic object to its target object type.
No comments:
Post a Comment