Class : A Class is a user defined data type with attributes methods types and events for a particular entity or business scenario or business application Class creation is of 2 steps: CLASS CL_CLASSNAME DEFINITION. ENDCLASS. CLASS CL_CLASSNAME IMPLEMENTATION. ENDCLASS.
OBJECTS Objects are instances of classes. Each object has a unique identity i.e. memory and its own attributes. Objects are created with the command CREATE OBJECT. Once an object is created, we have an instance of class with separate memory Syntax : It is also 2 step process STEP1 : DATA: instance TYPE REF TO CL_CLASSNAME. STEP2 : CREATE OBJECT instance.
Components of a Class
constants declared within a class are called as attributes of the class. Methods:.
Components of a Class : Attributes:. These are similar to function modules in ABAP. Methods can access all of the attributes of a class.Any data variables). Methods are called using the CALL METHOD statement.
. providing some functionality for a class. Methods are defined in the definition part of a class and Implemented in the implementation part.Methods contain a block of code.
Remaining things will be discussed later. Each event must be linked to a method for writing the corresponding business logic for the action/event raised by the user . These are mainly used to extend the scope or functionality of the class.
. Interfaces:Interfaces are similar to classes which contain methods Without any implementation.
Events:Events are like actions performed by the user.
Instance and Static Components
that means all objects with be referring to same/fixed memory.
. These are referred using the symbol ‘ ’.
Static components : The components which exists globally
or fixed for all instances(objects) of the class are called as STATIC components. instance components will be available separately. that means for each object.
Instance components : The components which
exist separately for each instance (object) of the class are called as instance components. These are referred to using the symbol ‘ =>’ .
Visibility of Components
. In ABAP Objects.
Each class component has a visibility. the whole class definition is separated into three visibility sections: PUBLIC PROTECTED PRIVATE.
Protected section: Data declared in the protected section can be accessed by the class itself.
. Private Section: Data declared in the private section can be accessed by the class only.
Public section: Data declared in public section can be accessed by the class itself. but not by its subclasses and by external users outside the class. and also by its subclasses but not by external users outside the class. by its subclasses as well as by other users outside the class.
Global Class and Local Class
Global Class and Local Class: Classes in ABAP Objects can be declared either globally or locally. Global Class: Global classes and interfaces are defined in the Class Builder (Transaction SE24) in the ABAP Workbench.
. All of the ABAP programs in an R/3 System can access the global classes Local Class: Local classes are define in an ABAP program (Transaction SE38) and can only be used in the program in which they are defined.
Ex on Class
Click on create Provide short description. Save and activate.
Steps to create a class:Go to SE24 Give the class name ZCL_sample. define an attribute as below V_name instance public type char25. Click on save button Go to attributes tab. A class is created by the above steps .
Eg1:. Next. Syntax: create object <object name> Eg1:. Eg2:-OBJ2 type ref to ZCL_sample. 1. Syntax :-<objname> Type ref to <class name>.
Creating instance or objects for the class:. First declare a variable for an object or instance.create object obj1 Eg2:.Create object obj2
. instantiate/create copy for the object. Creation of objects is of 2 steps.OBJ1 type ref to ZCL_sample. 2.
Accessing class components
Syntax: <Objname> <class-component name>
E. use the below Syntax.OBJ1 v_name OBJ2 v_name .
Accessing or calling class components:To call any class component with the instance.g:.
Example program :Data: OBJ1 type ref to ZCL_sample Create object OBJ1 *set a value using obj1 Obj1 v_name = ‘IBM INDIA’. *print a value using obj1. Write:/ obj1 v_name color 5.
Similarly create another object Create object obj2 *Set a value using OBJ2 Obj2 v_name = ‘Mahindra –satyam’.
. *print a value using obj2 Write: / obj2 v_name color 6.
*Changing the value using obj1 Obj1 v_name = ‘IBM-INDIA-Hyderabad loc’. *Print a value using obj1. Write: / obj1 v_name color 6. *Print a value using obj2. Write: / obj2 v_name color 5. NOTE: - If we change the v_name value using obj1,it will not affect obj2 since both have separate memory areas
. Click on ‘attributes tab Declare a static variable as below.
Steps: Go to se24 Give the class name as ZCL_SAMPLE.
Save and activate. Click on change button.
Print a value using obj1&obj2 .
Write the program as below. Data : obj1 type ref to zcl_sample . Write : / obj2 → v_name2 . Obj1→ v_name2 = `IBM INDIA HYD’ . Create object obj2 .
. *Set a value using obj1 . Write : / obj1 → v_name2 . Data : obj2 type ref to zcl_sample . Create object obj1 . *Now. Write : / sy_uline .
Write : / obj1 → v_name2 . *Print a value using obj1&obj2 .
. Obj2→ v_name2 = `TCS HYD’ . Write : / ZCL_SAMPLE => V_NAME2 .
*Set a value using obj2 . Write : / obj1 → v_name2 . Second way of using static members ZCL_SAMPLE => V_NAME2 = ‘IBM INDIA’ .
Never use static variables with objects.
. it will be reflect remaining objects also because all the objects will be referring to the same memory.
Note All the static variables will have a global memory . we use static variables with the class name only. Syntax :<class name> => < static variable name> Eg :. In the real time . It is always preferred to call the v using the class name.zcl_sample => v_name1 = ‘ARJUN’. Static variables must be accessed by symbol ‘=>’ . If we change the static variable using object1.
. private .
Steps: Go to se24 Give the class name as ZCL_SAMPLE.
. Click on create Declare the variables/Attributes as below
Save and activate the class.
. OBJ1→ Pr_name = ‘ARJUN’→ Error B’coz Pr_name is a private Variable OBJ1→ Po_name = ‘ARJUN’→Error B’coz Po_name is a protected variable. OBJ1→ Pu_name = ‘ARJUN’ →No error B’coz Pu_name is variable. Create object obj1.
Write the code as below.
. Give the method name as GET_CUST_DET GET_CUST_DET INSTANCE PUBLIC Click on the ‘ parameters’ button and & specify the parameters as below…. EX_KNA1 EXPORTING TYPE KNA1. Click on the ‘methods’ tab. Give the class name as ZCL_sample. IM_KUNNR IMPORTING TYPE KNA1-KUNNR.
STEPS: Go to se24.
Select single * from kna1 Into corresponding ields of EX_KNA1 Where KUNNR = IM_KUNNR. Endmethod.
save and activate the class.
Click on the button ‘code’.
double click on the method name &write the below code.
All the instance members will be visible now. Click on ‘create Instance’ button. The output will be displayed.
Testing the Method : Click on ‘execute’ button.’ Click on the ‘execute’ button for the method GET_CUST_DET.
Calling the Method from the program
select the radio button call method and provide the values as below. Call method Instance : OBJ1 Class : ZCL_Sample
Method : GET_CUST_DET.Code in abap program :
Data: wa_kna1 type kna1. Select ‘ABAP object patterns’. And press enter
. PARAMETERS : P_KUNNR TYPE KNA1-KUNNR. Data: OBJ1 type ref to zcl_sample. Create object OBJ1. Click on PATTERN button.
EXPORTING IM_KUNNR = P_KUNNR.
The below code will be displayed automatically. IMPORTING EX_KNA1 = WA_KNA1. WA_KNA1-NAME1. WRITE : / WA_KNA1-KUNNR . Call method OBJ1 GET_CUST_DET.
. Save Activate Test it.
Methods using table types
That’s why this option is not available. If we want to use tables parameters.
. we have an option by name TABLE where we can specify the internal tables directly. we don’t have any option for specifying internal tables directly. Whereas in function modules. Tables are absolute in OO-ABAP.
TABLES PARAMETER: In the methods. then we need to create a table type in data dictionary.
Select ‘table type’ Give short description. The table type is created
. Give the data type as zkna1_t.
Steps to create a table type: Go to se11. Click on the button ‘create’. Give the line type as kna1 Save & activate.
GET_CUST_LIST Instance Public Click on the ‘parameters’ button. Give the class name. Click on ‘methods’ tab.
Creating a method using table types: Go to se24.
. IM_LAND1 IMPORTING TYPE KNA1-LAND1. Define a method as below. Define the parameters as below. EX_KNA1 EXPORTING TYPE ZKNA1_T.
Write the below code.activate & test it. METHOD GET_CUST_LIST SELECT * FROM KNA1 INTO TABLE EX_KNA1 WHERE LAND1 = IM_LAND1. Save . ENDMETHOD.
. CALL METHOD OBJ GET_CUST_LIST. PARAMETERS: P_LAND1 TYPE KNA1-LAND1.
Using the method in the program: DATA: I_KNA1 TYPE ZKNA1_T. CREATE OBJECT OBJ1. DATA: WA_KNA1 LIKE LINE OF I_KNA1. EXPORTING IM_LAND1 = P_LAND1 IMPORTING EX_KNA1 = I_KNA1. DATA:OBJ1 TYPE REF TO ZCL_SAMPLE.
WA_KNA1-LAND1.NAME1. WA_KNA1. WRITE:/ WA_KNA1-KUNNR.
LOOP AT I_KNA1 INTO WA_KNA1. ENDLOOP.
USER-DEFINED TYPES IN THE CLASSES
Click on the ‘TYPES’ Tab. Define type TY_KNA1 as below
Click on the button
and write the below code
USING USER-DEFINED TYPES IN THE CLASSES: Open the Class ZCL_Sample.
Save and activate the class.
. End Method.
Define a method as below
Define the below Parameters
Write the below code Method GET_CUST_DETAILS Select single * from KNA1 into corresponding fields of EX_KNA1 where KUNNR = IM_KUNNR.
Save. Activate and Test it.
DATA: OBJ1 TYPE REF TO ZCL_SAMPLE Create object OBJ1. WRITE:/ WA_KNA1 – KUNNR.
USING THE METHOD IN THE PROGRAM: DATA: WA_KNA1 TYPE ZCL_SAMPLE =>TY_KNA1 B’coz TY_KNA1 is a static member.
Parameters: P_KUNNR TYPE KNA1 – KUNNR CALL METHOD OBJ1 GET_CUST_DETAILS
Exporting IM _KUNNR = P_KUNNR Importing EX_KNA1 = WA_KNA1. Activate & Test it .
. Save. WA_KNA1 – LAND1. WA_KNA1 – NAME1.
EVENTS IN OO ABAP
Each event must be linked to a method for writing the corresponding business logic for the action/event raised by the user .
Event: Events are like actions performed by the user. Raise the event in any method implementation. 5 STEPS TO BE FOLLOWED WHEN WORKING WITH THE EVENTS: Define an event . Link event and method and convert the method into event-handler method.
. Define a method for writing business logic for the action/event raised by the user . Use set handler and register event handler method to a particular instance in the program.
SET HANDLER: It is the keyword which is used to register event handler method to a particular instance of class where the event is raised. but for all the instances of the class. For All Instances Sometimes we want to execute event handler methods not to a particular instance of class. then we use FOR ALL INSTANCES.
TYPES OF EVENTS
There are 2 types of events:
Standard Events : Events which are defined and raised by SAP are called
Standard Events Ex : Double_Click. raised and also handled by
creating event handler methods are called Custom Events .
Custom Events : Events which are defined. Right_Click For these events we are suppose to create event handler methods and write the corresponding logic. Events contain only exporting parameters which are imported by event handler methods.
Ex On Events
Click on ‘events’ tab. NO_KUNNR INSTANCE PUBLIC Click on save.
Step1: STEPS TO DEFINE AN EVENT:Open the class zcl-sample. Define an event as below.
EVT_HDLER_METH instance public.
. Write the below code in the method.
Step2: STEPS TO DEFINE A METHOD:Click on ‘methods’ tab Give the method name as below . Click on save. Write:/’please enter customer number’.
Event : NO-KUNNR. Click on the ‘ detail view ‘ icon A pop-up is rised.
.e EVT_HDLER_METH .
Click on the change button. The symbol >> will indicate that it is a event handler method. Select the checkbox event handler &provide the class name and event name Event handler for class/interface : ZCL-SAMPLE. which will convert the method into event handler method.
Step3: STEPS TO LIK EVENT AND METHOD: Put the cursor on the Method i.
If im_kunnr is initial .
Endmethod. Select single * from kna1 Into corresponding ields of EX_KNA1 Where KUNNR = IM_KUNNR. Else. Raise event NO_KUNNR.
Step4: Create a triggering method which will raise the event. SYNTAX TO RAISE AN EVENT: SYNTAX: RAISE EVENT<event name> Example: Raise event <xyz>. Ex: Modify the method as below GET_CUST_DET as below: METHOD GET_CUST_DET.
Step5: Use set handler and register event handler method to a particular instance where the event is raised. Create object OBJ1. Set handler OBJ1 EVT-HDLER-METH for obj1.
SYNTAX FOR SET HANDLER: SET HANDLER <event-handler method> FOR <instance> Ex:.
.. Parameters: P_kunnr type kna1-kunnr.
Data: wa_kna1 type kna1. Data:obj1-type ref to zcl-sample.Set handler OBJ1 XYZ FOR OBJ1
Ex: Go to the program and call the method without giving any customer no.
Call method OBJ1-GET_CUST_DET. EXPORTING Im_kunnr = P_kunnr IMPORTING Ex_kna1 = wa_kna1. Write:/wa_kna1 –kunnr,wa_kna1-name1. The output is : Event NO_KUNNR is raised.
Constructors are special type of methods. constructor methods are executed automatically whenever a object is created or Whenever a class is loaded. These methods are mainly used to set default values in a class. i.e. Used to default values to attributes. (Ex V_Werks = ‘0001’) These are declared only in public section These methods cannot be called explicitly
Types of CONSTRUCTORS
There are 2 Types of constructors :
INSTANCE Constructor STATIC Constructor
These are special type of methods. These methods have only importing parameters. constructor method is executed automatically whenever a object is created or instantiated. These methods are mainly used to set default values in a class. The name of the constructor method is ‘constructor’.
. There are no exporting parameters.
A method by name ‘constructor’ will be added automatically with the symbol . Double click on the constructor & write the code. Click on button constructor available at the top right corner.
Click on the parameters button & define the importing parameters . Save and activate. Im_kunnr---->type-- kna1-kunnr.
EXAMPLE ON CONSTRUCTORS:
Open the class zcl_sample.
CALLING CONSTRUCTORS IN ABAP PROG:
We never call the constructor method bcoz they are executed automatically whenever an object is created.
STEPS TO CREATE OBJECT:
Click on the pattern button. Select ‘abap object patterns’. Select create object & provide instance name & class name. Instance : obj1 Class : zcl_sample.
Press enter… the code will be generated as below. Create object obj1 Exporting Im_kunnr=’123’. Execute the program.
REAL TIME EXAMPLE ON CONSTRUCTORS:
The class by name cl_gui_alv_grid has a constructor method by name ‘constructor’. This method needs an importing parameter by name i_parent. This parameter takes the reference of custom container as a default value to display alv.
Constructor method is called for each instance created i.E.., Create 5 instances ,the constructor method is called 5 times.
These are executed only once..i.e.
. Ex: obj1-- get_cust_det. The first call can be through instance or class name.STATIC CONSTRUCTORS:
It is a type of constructor method. This method is executed automatically whenever a first call to the class is made. The static constructors will not have any importing / exporting parameters . Static constructor are used to set the default values globally .Irrespective of instances. Zcl_sample-- v_name2. The name of the static constructor is CLASS_ CONSTRUCTOR.
A method by name CLASS_CONSTRUCTOR will be created. Save it & activate and Execute the abap program. O/P :The default customer is:123(from static constructor) Default customer is 123.
STEPS TO DEFINE STATIC CONSTRUCTORS: Open the class ZCL_SAMPLE. Write the below code in the method. Method class_constructor Write : / endmethod.
. Click on the button CLASS_CONSTRUCTOR available at the top right corner.(From instance constructor).
Interface Interfaces are independent structure which are used in a class to extend the functionality of a class. Just give the name of interface under the ‘interface’ tab. All the interface methods will be automatically copied to the classes in a particular class without effecting the other classes. We need to define an interface with the required method names in SE24 TCODE. Interfaces contains methods without any implementation. Now. Where as a class contains methods with implementation.this interface can be used by ‘n’ no of classes to extend the functionality of the class.
In all the classes.the methods names are same but the implementation will be different from one class to another class thereby achieving the concept called POLYMORPHSM. Polymorphism is implemented in the from of interface.
e each method will have its own implementation in different different classes but with the same name.
Polymorphism It is a concept by which the same method names will behave differently in different classes i.
creating an interface
Click on the ‘method ‘ tab. Click on create. Select the ‘interface ‘ radio button. Save &activate the interface. Declare 2 methods as below.
Steps to create an interface: Go to SE24. Get_mara with im_mara as importing parameter and ex_mara as exporting parameter Get_KNA1 with im_kunnr as importing parameter and ex_KNA1 as exporting parameter We cannot write any ABAP code in the interface. Give the interface name as ZV_IF.
Using interface in the classes
ZV_IF~ get_KNA1. Go to the ‘interface’ tab. Click on the methods tab .
. Double click on the method ZV_IF~get_mara & write the below code. The interface methods will be copied automatically as below ZV_IF~get _mara. Give the class name as ZCL_sample.
Go to SE24. Give the interface name as ZV_IE and Press enter.
. Select single * from mara. Into corresponding fieldsof EX_mara Where mara =IM_mara.
Method ZV_IFN get_mara. Save . End method.active &test it. Similarly write the code for method ZV_IF~get_KNA1.
CALL METHOD OBJ1 ZV_IF~GET_MARA. WRITE : / WA_MARA-MATNR. Parameters : p_matnr type mara_matnr.
Using interface method
DATA : wa_mara type mara. EXPORTING IM_LAND1 = P_MATNR IMPORTING EX_MARA = WA_MARA. Create object OBJ1. WA_MARA-MEINS. DATA : OBJ1 type ref to zc__sample.
This means we can add additional features to an existing class without modifying it. You can use an existing class to derive a new class. The class which receives the properties is called SUB class or Child class. There is no multiple inheritance. Derived class can overwrite the existing methods also add new code. Advantage of this property is reusability. You can access the methods and attributes the super class using this word super. ‘REDIFINATION’ is the keyword which is used to overwrite the parent class methods with new definition. The class which passes the properties is called SUPER class or Parent class. Derived class inherits the data and methods of a super class. NOTE: In oo_ABAP. we have only single inheritance. Super is the keyword used to represent the super class.
Inheritance Inheritance is the concept of passing the properties of one class to another class. But we have Multilevel inheritance
e each method will have its own implementation in different different classes but with the same name. Polymorphism behavior is implemented in the form of Interface Methods Method overloading Method Overriding.
Polymorphism It is a concept by which the same method names will behave differently in different classes i.
imp/exp parameters is called method overloading It is not supported in OO-Abap. Method Overriding : A child class inherits the super class methods And overwrites them with extra functionality is called Method Overriding.e. Redefinition is the keyword which is used to overwrite the super class method.
Interface Methods : The Interface Methods can be implemented in multiple classes with same name but with diff implementation which is called as polymorphism.
. Method overloading : Methods with same name but different signatures i.
Ex on Inheritance
STEP1: create a super class with a method.active the class. Select single * from mara into corresponding fields of ex_mara Where matnr = im_matnr End method. Write the belowcode in the method. Remove the checkbox final because we want to inherit the class in a child class
Define a method by name Get_material_data with in_matnr and ex_mara as parameters. Create a class by name ZCL_SUPER. Save.
Click on ’create’ button.
Step2: Create child class Go to SE24. Give the class name as ZCL_CHILD. The method Get_material_Data will be copied automatically as it is a super class method.
Click on save. Click on the icon below and provide the super class name as ZCL_SUPER. Select the method name & click on the Redefine icon So that we can redifine super class method with extra functionality
Then. Save & activate the child class. Write your own or extra code as below. Uncomment the code if you need. The coding will be generated automatically. Declare wa_makt under attributes tab.
. Now. Select single* from makt into corresponding fields of wa_makt Where matnr = im_matnr And spras=’EN’.
Now .double click on method & write the below code.
The method should look as below:
DATA : Wa_makt type makt. DATA : Wa_mara type mara.
. Call method OBJ Get material_Data. Exporting. Im_matnr = P_matnr.
Using Child Class In the Program DATA : OBJ type Ref to ZCL_CHLD. Importing Ex_mara = wa_mara. Create object OBJ. PARAMETERS : P_matnr type mara_matnr.
Wa_mara-meins . *Display description Data Write : / OBJ wa_makt_matnr. OBJ wa_makt_maktx
. OBJ wa_makt_spras. Wa_mara-mtart.
*Display material master data Write: / wa_mara-matnr.
Abstract class are mainly used for creating inheritance.
Abstract class It is a class which contains methods with implementation as well as methods without implementation . Note :.we cannot create an object to the abstract class because they are not fully implemented. Instead create an object to the child class which inherits the abstract class and then call the methods . (or) Abstract class is a class which contains at least one abstract method Abstract method :It is a method which doesn’t contain any implementation.
Click on Create Remove the checkbox final as below.
steps to define an Abstract class :Goto SE24 Give class by name ZCL_ ABSTRACT .
Double click on the method and write the code.
Define the method by name Get_mara_data with Im_mara as importing parameter and Ex_mara as exporting parameter.
Click on the detail view button as below
A pop is raised .
Create another method by name Get_kna1_data with Im_kunnr as importing parameter and Ex_kna1 as parameters .select the checkbox abstract. Click on CHANGE button Now the method is converted into abstract method . Put the cursor on the method Get_kna1_data .
Now the class has 2 methods as below :
Creating inheritance for the Abstract class
Select the abstract method Get_kna1_data and click on the ‘Redefine’ button and write the abap code. Click on Create Click on create Inheritance button Give the super class name as ZCL_ ABSTRACT.
Steps: Go to SE24 Give class by name ZCL_ ABSTRACT_CHILD . The super class methods will be automatically copied .
Importing Ex_kna1 = wa_kna1. wa_kna1-name1. Call method OBJ→ get_kna1_data Exporting im_kunnr =p_kunnr.
. Data: OBJ type ref to ZCL_ ABSTRACT_CHILD . Write :/ wa_kna1-kunnr .
Data : wa_kna1 type kna1. Create object OBJ .
Select single *from kna1 Into wa_kna1 where kunnr = im_kunnr.create an object to the child class as we cannot create the objects to the abstract class . Using abstract classes in programs :Note :. Parameters : p_kunnr type kna1-kunnr.
LOCAL CLASS : Defining a class inside a program is called local class Class creation is of 2 steps: CLASS CL_CLASSNAME DEFINITION. USING STATIC AND INSTANCE COMPONENTS IN LOCAL CLASS
. ENDCLASS. CLASS CL_CLASSNAME IMPLEMENTATION.
USING PUBLIC.PROTECTED. DATA : CLASS-DATA METHODS: CLASS-METHODS EVENTS CLASS-EVENTS Protected-section . DATA : CLASS-DATA METHODS: CLASS-METHODS EVENTS CLASS-EVENTS
. Public-section .PRIVATE VISIBILITY: Syntax : CLASS CL_CLASSNAME DEFINITION.
Private-section . DATA : CLASS-DATA METHODS: CLASS-METHODS EVENTS CLASS-EVENTS ENDCLASS.
PUBLIC SECTION. END CLASS.Syntax to Define an Event CLASS lcl_sample DEFINITION.
USING EVENTS IN LOCAL CLASSES: 1. EVENTS : <EVENTNAME>.
Syntax to Define event handler method CLASS lcl_sample DEFINITION.
2. METHODS : <EVENT HANDLER METH> FOR EVENT <EVENTNAME> OF <CLASSNAME> END CLASS
CLASS <childclassname> DEFINITION INHERITING ROM <SUPERCLASS>. METHODS : <Methodname> END CLASS. METHODS : <Methodname> REDIFINATION END CLASS.
USING INHERITANCE IN LOCAL CLASS CLASS <superclassname> DEFINITION.
DEFINATION DEFERRED : It is the keyword which indicates the class definition is delayed or postponed or Defined at some place in program. Syntax : CLASS <Classname> DEFINITION DEFERED.
LOCAL CLASS WITH ATTRIBUTES
LOCAL CLASS WITH METHODS
LOCAL CLASS WITH EVENTS
LOCAL CLASS WITH INHERITANCE
OOPS ALV PROGRAMS
Introduction: The ALV grid data is displayed on an UI element called as ‘custom container’.
We can display more than one ALV grid data on a single screen.
Advantages of OO ALV : We have ‘n’ no of events available in the classes when compared to ALV with function modules which gives flexibility for the programmer to develop ALV’S for various scenarios. Radiobutton on the same screen in addition ALV grid data. We can also place different UI elements like checkbox.
. The ALV grid data is displayed in the form of custom container with which we can control the size of ALV grid Whereas we cannot control the size of the ALV with function Modules.
CLASSES USED IN OO ALV’S
The most commonly used method to display ALV grid data is.Most commonly used classes are :
CL_GUI_ALV_Grid CL_GUI_CUSTOM_CONTAINER CL_dd_document CL_GUI_ALV_tree_simple CL_GUI_container CL_GUI_splitter_container.
OOPS ALV with Structure.1.
Steps for a simple OO-ALV:
STEP1: Create data declarations for ALV/custom container. Double click on 100 and create a screen
STEP3:Goto screen layout and drag and drop a custom container. CALL SCREEN 100. Give the name as ALV_CONT. START-OF-SELECTION .
STEP2 : Create screen 100. STEP4:Click on flow-logic and create the PBO Module and write the below code . DATA : ALV_GRID TYPE REF TO CL_GUI_ALV_GRID DATA : I_MARA TYPE TABLE OF MARA . DATA : WA_MARA TYPE MARA . DATA : ALV_CONT TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
ENDMODULE. PERFORM GET_DATA. Endform.
. CREATE OBJECT ALV_GRID EXPORTING I_PARENT = ALV_CONT.
MODULE STATUS_0100 OUTPUT. CREATE OBJECT ALV_CONT EXPORTING CONTAINER_NAME = 'ALV_CONT'. PERFORM DISPLAY_ALV . PERFORM CREATE_OBJECTS. STEP5 : Define each subroutine as below: FORM CREATE_OBJECTS .
FORM GET_DATA . SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS ENDFORM. FORM DISPLAY_ALV . = I_MARA. Save->activate->test it.
. CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY EXPORTING I_STRUCTURE_NAME = 'MARA' CHANGING IT_OUTTAB ENDFORM.
OOPS ALV with Fieldcatelog
DATA : i_mara TYPE TABLE OF ty_mara . DATA : i_fcat TYPE lvc_t_fcat .
Same steps as previous program with additional changes as below.
. DATA : alv_cont TYPE REF TO cl_gui_custom_container. DATA : alv_grid TYPE REF TO cl_gui_alv_grid . END OF ty_mara. mtart TYPE mara-mtart. meins TYPE mara-meins. DATA : wa_mara TYPE ty_mara . mbrsh TYPE mara-mbrsh. TYPES : BEGIN OF ty_mara. matnr TYPE mara-matnr. DATA : wa_fcat LIKE LINE OF i_fcat . STEP1: Create data declarations for ALV/custom container.
PERFORM create_fcat .
PERFORM CREATE_OBJECTS. PERFORM DISPLAY_ALV .
. PERFORM GET_DATA.
STEP2 : Create screen 100. STEP3: Design screen with custom container STEP4:Click on flow-logic and create the PBO Module and write below code
MODULE STATUS_0100 OUTPUT.
CLEAR wa_fcat . wa_fcat-fieldname = 'MATNR' . APPEND wa_fcat TO i_fcat . wa_fcat-tabname = 'I_MARA' . wa_fcat-col_pos = '1' . ENDFORM
STEP5 : Define each subroutine as previous program . Define field catalog subroutine as below: FORM create_fcat . wa_fcat-SCRTEXT_M = 'Material No' .
Finally display ALV with fieldcatelog instead of structure. CALL METHOD alv_grid->set_table_for_first_display CHANGING it_outtab = i_mara
it_fieldcatalog = i_fcat. FORM display_alv .
METHODS : handle_double_click FOR EVENT double_click OF cl_gui_alv_grid IMPORTING e_row. CLASS lcl_event_handler DEFINITION . we need to define a LOCAL CLASS as below. For this event. we need to define a event handler method as below. we need to define a event handler method. ENDCLASS
The E_ROW contains the importing parameters. to define a event handler method. Using this we can write our own logic
. We have the event DOUBLE_CLICK in class CL_GUI_ALV_GRID.
For Interactive ALV’S. CLASS lcl_event_handler DEFINITION . PUBLIC SECTION . So. E_ROW-INDEX gives the index or row no where double click event is raised. ENDCLASS Once we define a LOCAL CLASS.
DATA : i_fcat TYPE lvc_t_fcat . DATA : wa_mara TYPE ty_mara .
Same steps as previous program with additional changes as below. also define object or local class. DATA : obj TYPE REF TO lcl_event_handler .
STEP1: Create data declarations for ALV/custom container. DATA : i_mara TYPE TABLE OF ty_mara . CLASS lcl_event_handler DEFINITION DEFERRED . DATA : i_makt TYPE TABLE OF ty_makt . DATA : i_fcat2 TYPE lvc_t_fcat . DATA : alv_grid TYPE REF TO cl_gui_alv_grid . DATA : wa_fcat2 LIKE LINE OF i_fcat . *BASIC ALV DATA : alv_cont TYPE REF TO cl_gui_custom_container. DATA : wa_makt TYPE ty_makt .
. DATA : wa_fcat LIKE LINE OF i_fcat . *SECONADARY LIST DATA : alv_cont2 TYPE REF TO cl_gui_custom_container. DATA : alv_grid2 TYPE REF TO cl_gui_alv_grid .
. PUBLIC SECTION . STEP3: Define and implement EVENT HANDLER method. ENDMTHOD. CLASS lcl_event_handler IMPLEMENTATION. ENDCLASS .
STEP2 : Define a local class and its implementation. METHODS : handle_double_click FOR EVENT double_click OF cl_gui_alv_grid IMPORTING e_row. METHOD handle_double_click . ENDCLASS. CLASS lcl_event_handler DEFINITION .
CREATE OBJECT OBJ. PERFORM create_fcat .
. create object for local class SET HANDLER obj->handle_double_click FOR alv_grid .
PERFORM CREATE_OBJECTS. PERFORM GET_DATA.
STEP4 : Create screen 100. STEP3: Design screen with custom container STEP4:Click on flow-logic and create the PBO Module and write below code
MODULE STATUS_0100 OUTPUT. “use set handler
ENDMODULE. PERFORM DISPLAY_ALV .
ENDMETHOD . METHOD handle_double_click . "LCL_EVENT_HANDL
. READ TABLE i_mara INTO wa_mara INDEX e_row-index .
CALL SCREEN 200.
Write the custom logic for DOUBLE_CLICK event CLASS lcl_event_handler IMPLEMENTATION. SELECT * FROM makt INTO CORRESPONDING FIELDS OF TABLE i_makt WHERE matnr = wa_mara-matnr .
Screen 200. Perform create_object2. Write the below code in PBO of screen 200. Perform create_Fcat2 Perform Display_ALV2
. Create screen 200. Place a custom container by name ALV_CONT2.
. Endif. ENDFORM. IF alv_cont2 is initial .
FORM create_objects2 . CREATE OBJECT alv_grid2 EXPORTING i_parent = alv_cont2. CREATE OBJECT alv_cont2 EXPORTING container_name = 'ALV_CONT2‘.
wa_fcat2-tabname = 'I_MAKT' . ENDFORM FORM display_alv2 . wa_fcat2-fieldname = 'MATNR' . CALL METHOD alv_grid2->set_table_for_first_display CHANGING it_outtab = i_makt
it_fieldcatalog = i_fcat2.
CLEAR wa_fcat2 . If I_FCAT IS INTIAL .
FORM create_fcat2 . wa_fcat2-scrtext_m = 'Material No' . ENDIF. wa_fcat2-col_pos = '1' . APPEND wa_fcat2 TO i_fcat2 .
INTERACTIVE ALV WITH 2 ALV GRIDS ON A SINGLE SCREEN
also define object or local class. STEP5: Design screen with 2 custom containers by name ALV_CONT1 & ALV_CONT2.
STEP1: Create data declarations for ALV/custom container. STEP6: Click on flow-logic and create the PBO Module and write below code
. STEP2 : Define a local class and its implementation. STEP3: Define and implement EVENT HANDLER method. STEP4 : Create screen 100.
ENDIF. PERFORM CREATE_OBJECTS2. create object for local class SET HANDLER obj->handle_double_click FOR alv_grid . PERFORM CREATE_FCAT2. PERFORM DISPLAY_ALV1 . PERFORM GET_DATA. PERFORM DISPLAY_ALV2.
IF ALV_CONT1 IS INITIAL. “use set handler ELSE. PERFORM CREATE_CAT1 . CREATE OBJECT OBJ. PERFORM CREATE_OBJECTS.
DISPLAYING LOGO & TOP OF PAGE IN OO-ALV
Below are the commonly used methods : Add .
CL-DD-DOCUMENT : It is the class used to display logo & top-of-page.picture New – line Add – gap Add – text Display – document Steps : STEP1 :Upload logo into SAP document server using OAER Tcode. STEP2: Create data declaration STEP3 : Create a screen 0100 STEP4: Place to custom containers by names ‘LOGO’ & ‘ALV_CONT‘
PERFORM GET_DATA. PERFORM DISPLAY_ALV1 .
STEP5: Write the bellow code in pbo PERFORM CREATE_OBJECTS. PERFORM DISP_LOGO. PERFORM CREATE_FCAT .
HEADING = CL_DD_DOCUMENT=>HEADING . CALL METHOD DD->ADD_PICTURE EXPORTING PICTURE_ID = 'ARJU' .
CALL METHOD DD->ADD_GAP EXPORTING WIDTH = '25' .
. CALL METHOD DD->ADD_TEXT EXPORTING TEXT = 'MATERIAL MASTER REPORT' = HEADING . DATA : HEADING TYPE SDYDO_ATTRIBUTE .
FORM DISP_LOGO .
CALL METHOD DD->DISPLAY_DOCUMENT EXPORTING CONTAINER ENDFORM. " DISP_LOGO
USING FINAL/FRIEND AND ALIAS
the SUPER class has to treat the child as a friend. To access the PRIVATE variables of a super class. we can create inheritance. It can access only PUBLIC&PROTECTED variables. go to the super class Go to ‘FRIENDS’ tab Enter child class name press Enter.
FRIENDS: By default . From now onwards the child class can access the PRIVATE variables of a SUPER class.
If ‘FINAL’ option is not selected. If FINAL option is selected.
FINAL:It is a keyword which specifies weather inheritance is possible or not. the child class cannot access the private variables of a SUPER class.
. To do this. So. ’FRIEND’ is a concept which is used to access the PRIVATE variables of a SUPER class. we cannot create inheritance (or) the class cannot be inherited.
Go to child class ZCL_CHILD. Save . Give child class name as ZCL_CHILD.
. Create a method by name Friend_Method. activate & test it. Obj PR_NAME = ‘IBM INDIA’. WRITE :/ obj PR_NAME. Save & activate. PR_NAME INSTANCE PRIVATE TYPE CHAR20. Go to ‘FRIENDS’ Tab. DATA: obj type REF TO ZCL_SUPER. Write the below code. Create a private variable.
Steps for using FRIEND concept:Go to Super class ZCL_SUPER. CREATE OBJECT obj.
. ALIASES:It is used to provide the alternative name for the interface methods. CRATE OBJECT obj. Save . Call method obj Friend_Method.
Create a ABAP program & write the below code. DATA: obj type REF TO ZCL_CHILD.activate & test it.
protected? In how many ways we can create a class? How do u create a method? Can we define user defined types in classes? What is a table type and what is its use?? What is an event?
What is a class?how do u create What is an object? how do u create What do u mean by attributes and methods?Any real time ex? What is the diff b/w instance and static comp?how do u access them? Explain? What is diff b/w private.public.
What is an event handler method?explain syntax? What is set handler? please explain syntax? How do u raise an event? Explain the concept of events?any real time ex? What is a constructor and types? Why do we use construcors? Ay real time ex? What is the diff b/w instance and static construcor? Can we define importing and exp parameters for constructors? Which constructor I s executed first? What is an Inteface?any real time ex?
then how? What is FINAL?
What is polymorphism? How do we use interfaces in classes? What is aliases? What is inheritance?explain syntax? What is super? What is redifination? Do w have multiple inheritance and mulit level inheritance? What is an abstract class? Can we create an object to abstract class? If no.
If yes how? How did u display logo/top of page in OOALV and normal ALV?
Can we access private variables of super class? If yes how?? Did you work on OOPS ALV? If yes explain? What are the advantages of OO ALV’S? What are the classes in OO ALV’S? What are the methods? What is custom container? What is a fieldcatelog? Did u work on interactive ALV? I f yes explain the procedure? Also explain the procedure of interactive ALV using FM’S? Can we display 2 grids on a single screen.
OOPS ALV WITH SPLITTER CONTAINER
PERFORM SPLIT_MAIN_CONT. CL_GUI_SPLITTER_CONTAINER. ENDMODULE. CL_GUI_CONTAINER. PERFORM DISP_ALV1 . STEP2 : Create screen 100. PERFORM DISP_ALV2. STEP3: Design screen with custom container by name ‘MAIN_CONT’ MODULE STATUS_0100 OUTPUT.
STEP1: Create data declarations for CL_GUI_CUSTOM_CONTAINER. STEP4:Click on flow-logic and create the PBO Module and write below code PERFORM CREATE_OBJECTS. CL_GUI_ALV_GRID.
DATA : O_GRID1 TYPE REF TO CL_GUI_ALV_GRID . STEP2 : START-OF-SELECTION . DATA : O_PART2 TYPE REF TO CL_GUI_CONTAINER. STEP3: Design screen with custom container by name ‘MAIN_CONT’
. DATA : O_SC TYPE REF TO CL_GUI_SPLITTER_CONTAINER.
STEP1 : DATA : O_CC TYPE REF TO cl_gui_custom_container . CALL SCREEN 0300. DATA : I_MARA TYPE TABLE OF MARA . DATA : O_PART1 TYPE REF TO CL_GUI_CONTAINER. DATA : O_GRID2 TYPE REF TO CL_GUI_ALV_GRID . DATA : I_MAKT TYPE TABLE OF MAKT .
PERFORM CREATE_OBJECTS. PERFORM DISP_ALV2. PERFORM DISP_ALV1.
. form CREATE_OBJECTS . endmodule.
STEP4: Click on flow-logic and create the PBO Module and write below code module STATUS_0300 output. CREATE OBJECT o_cc EXPORTING container_name = 'MAIN_CONT'. * SET PF-STATUS 'xxxxxxxx'. endform. PERFORM SPLIT_MAIN_CONT. * SET TITLEBAR 'xxx'.
form SPLIT_MAIN_CONT . CREATE OBJECT o_sc EXPORTING parent = O_CC rows = 2 columns = 1
CALL METHOD o_sc->get_container EXPORTING row =1 column = 1 RECEIVING container = O_PART1 CALL METHOD o_sc->get_container EXPORTING row =2 column = 1 RECEIVING container = O_PART2
endform. CREATE OBJECT o_grid1 EXPORTING i_parent = O_PART1 .
form DISP_ALV1 . SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS . CALL METHOD o_grid1->set_table_for_first_display EXPORTING i_structure_name = 'MARA' CHANGING it_outtab = I_MARA .
form DISP_ALV2 . CALL METHOD o_grid2->set_table_for_first_display EXPORTING i_structure_name = 'MAKT' CHANGING it_outtab = I_MAKT . SELECT * FROM MAKT INTO TABLE I_MAKT UP TO 100 ROWS . endform. CREATE OBJECT o_grid2 EXPORTING i_parent = O_PART2 .
OO ALV WITH TREE
STEP2 : Create screen 100.
.OO ALV WITH TREE
STEP1: Create data declarations for ALV/custom container. ENDMODULE. PERFORM GET_DATA. PERFORM CALC_SUBTOTALS. STEP3: Design screen with custom container with name as ‘TREE’ STEP4:Click on flow-logic and create the PBO Module and write below code MODULE STATUS_0100 OUTPUT. PERFORM create_fcat . PERFORM CREATE_OBJECTS. PERFORM DISPLAY_ALV .
data : i_fcat type lvc_t_fcat . data : wa_bseg type bseg . start-of-selection .
. data : o_cc type ref to cl_gui_custom_container . STEP2: Create screen 100. call screen 100. data : i_sort type lvc_t_sort . data : wa_sort like line of i_sort . data : i_bseg type table of bseg . data : wa_fcat like line of i_fcat .
STEP1: data : o_tree type ref to cl_gui_alv_tree_simple .
PERFORM get_data. PERFORM create_objects .
STEP3: Design screen with custom container with name as ‘TREE’ STEP4: Click on flow-logic and create the PBO Module and write below code MODULE status_0100 OUTPUT. PERFORM create_fcat .
. PERFORM calc_subtotals . ENDMODULE. * SET PF-STATUS 'xxxxxxxx'. * SET TITLEBAR 'xxx'. PERFORM display_tree .
SELECT * FROM bseg INTO TABLE i_bseg UP TO 100 ROWS . ENDFORM. CREATE OBJECT o_tree EXPORTING i_parent = o_cc .
FORM get_data .
FORM create_objects . CREATE OBJECT o_cc EXPORTING container_name = 'TREE' . ENDFORM.
ENDCASE . ENDLOOP . MODIFY i_fcat FROM wa_fcat INDEX sy-tabix . LOOP AT i_fcat INTO wa_fcat . CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' EXPORTING i_structure_name = 'BSEG' CHANGING ct_fieldcat = i_fcat. WHEN 'DMBTR' . CASE wa_fcat-fieldname .
FORM create_fcat . wa_fcat-do_sum = 'X' . ENDFORM.
APPEND wa_sort TO i_sort .
. wa_sort-spos = '2' . wa_sort-spos = '1' . wa_sort-fieldname = 'BUKRS' . wa_sort-spos = '3' . wa_sort-subtot = 'X' . wa_sort-up = 'X' . wa_sort-up = 'X' . wa_sort-fieldname = 'BELNR' .
FORM calc_subtotals . APPEND wa_sort TO i_sort . wa_sort-subtot = 'X' . wa_sort-fieldname = 'GJAHR' . wa_sort-up = 'X' . APPEND wa_sort TO i_sort . ENDFORM.
FORM display_tree . ENDFORM. CALL METHOD o_tree->set_table_for_first_display CHANGING it_outtab = i_bseg it_fieldcatalog = i_fcat it_sort = i_sort.
. CALL METHOD o_tree->expand_tree EXPORTING i_level = '1'.
Smart Forms .THANK YOU
© SAP AG 2001.the Form Printing Solution.
Claudia Binder / Jens Stumpe 188