Sie sind auf Seite 1von 14

OVERRIDING

Purpose to redefine methods: if the method implementation at the super class is not satisfies the
requirement of the object which is created with reference to the sub class.

Principles of redefining methods:

1. The REDEFINITION statement for the inherited method must be in the same SECTION as the
definition of the original method.
2. If you redefine a method, you do not need to enter its interface again in the subclass, but only the
name of the method.
3. In the case of redefined methods, changing the interface (overloading) is not permitted;
exception: Overloading is possible with the constructor
4. Within the redefined method, you can access components of the direct super class using the
SUPER reference.
5. The pseudo-reference super can only be used in redefined methods.

6.
7.

Final Class: A class that is defined as final class can not be inherited further. All
Methods of a final class are inherently final and must not be declared as final in the
class definition. Also, a final method can not be redefined further.
If only a method of a class is final then that class can be inherited but that method
cannot be redefined.

Use of final class:


If you don't want anyone else to change or override the functionality of your class then
you can define it as final. Thus no one can inherit and modify the features of this class.

*TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.
Step 2: Add totals

To add totals we need to use GET_AGGREGATIONS, once we get aggregations instance, we


need to add aggregation by passing column name and aggregation type to method
ADD_AGGREGATION.
DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.
LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
* Add TOTAL for COLUMN NETWR
TRY.
CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
EXPORTING
COLUMNNAME = 'MENGE' "aggregation column name
AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

CATCH CX_SALV_DATA_ERROR . "#EC NO_HANDLER


CATCH CX_SALV_NOT_FOUND . "#EC NO_HANDLER
CATCH CX_SALV_EXISTING . "#EC NO_HANDLER
ENDTRY.

Step 3: Add subtotals

To add subtotals, we need to add sort to the columns and then we have to use SET_SUBTOTAL
method to display subtotals.
DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
CALL METHOD LR_ALV->GET_SORTS "get sorts
RECEIVING
VALUE = LR_SORT.
*TRY.
DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
CALL METHOD LR_SORT->ADD_SORT "add column sort
EXPORTING
COLUMNNAME = 'EBELN' "sort column always keyfield
* POSITION =
* SEQUENCE = IF_SALV_C_SORT=>SORT_UP
* SUBTOTAL = IF_SALV_C_BOOL_SAP=>FALSE
* GROUP = IF_SALV_C_SORT=>GROUP_NONE
* OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
RECEIVING
VALUE = LR_SORT_COLUMN.

* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.

Step 4: Add top of page(top of list) and end of list.

To add top of page and end of list we need to create a layout gird and insert that grid into ALV
using method SET_TOP_OF_LIST.

Create layout grid.


DATA : LR_HEADER TYPE REF TO CL_SALV_FORM_ELEMENT.
DATA: LR_GRID_LAYOUT TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
LR_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
LR_TEXT TYPE REF TO CL_SALV_FORM_TEXT,
L_TEXT TYPE STRING.
CREATE OBJECT LR_GRID_LAYOUT.
L_TEXT = 'Purchase Order Report' .
LR_GRID_LAYOUT->CREATE_HEADER_INFORMATION(
ROW = 1
COLUMN = 3
TEXT = L_TEXT
TOOLTIP = L_TEXT ).
LR_GRID_LAYOUT->ADD_ROW( ).
* LR_GRID_LAYOUT_1 = LR_GRID_LAYOUT->CREATE_GRID(
* ROW = 3
* COLUMN = 1 ).
LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
ROW = 2
COLUMN = 1
TEXT = 'Number of records found: '
TOOLTIP = 'Number of records found for your query' ).
LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
ROW = 2
COLUMN = 2
TEXT = LV_CNT
TOOLTIP = LV_CNT ).
LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
ROW = 3
COLUMN = 1
TEXT = 'Date : '
TOOLTIP = 'Date' ).
L_TEXT = SY-DATUM.
LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
ROW = 3
COLUMN = 2
TEXT = L_TEXT
TOOLTIP = L_TEXT ).
LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
LR_HEADER = LR_GRID_LAYOUT.

Create top of list.

CALL METHOD LR_ALV->SET_TOP_OF_LIST


EXPORTING
VALUE = LR_HEADER.

Step 5: Add End Of List

End of list can be created using method SET_END_OF_LIST.


DATA: LR_FOOTER TYPE REF TO CL_SALV_FORM_HEADER_INFO.
CLEAR L_TEXT.
L_TEXT = 'End of List as Footer'.
CREATE OBJECT LR_FOOTER
EXPORTING
TEXT = L_TEXT
TOOLTIP = L_TEXT.
LR_ALV->SET_END_OF_LIST( LR_FOOTER ).

Step 6: Display ALV

LR_ALV->DISPLAY( ). "display ALV

Use of ME Keyword
Step1. Create a proram in Tx- SE38.

Step2. Execute the program and we find the below output.

Step3. Now declare a local variable within the method with the same name as the class
variable & execute it.
Step4. So if the class and method contains the variable with the same name then priority is
given to the local one.

Step5. To access the class variable in the method which already contains a local variable
with the same name ME keyword is used which always points to the Current calling Object
of the class. Execute the program.
Step6. The O/P is as follows:

Use of SUPER keyword


Step1. Create a program and define the constructor in both the super and sub class.
Constructor is always executed form the super class to the Sub class in OO concept. So if
the object of the Sub class is created, then constructor of the sub class is called first and
within this sub class constructor method using the SUPER keyword the super class
constructor should be called other wise we ll receive a compile time error. So mostly
SUPER keyword is used in Inheritance concept where we have same method in both the
SUPER & SUB class. Execute the program.
Step2. The O/P is as follows:

Step3. Let’s define a method in the super class and redefine it in the subclass as well. Call
the Subclass method. Here calling the super class redefined method is optional but in case
of constructor it is mandatory to call the super class constructor in the subclass
constructor.
Step4. The O/P is as follows:

EDITABLE ALV

*&---------------------------------------------------------------------*
*& Report YKC_ALV_OOPS
*&
*&---------------------------------------------------------------------*
*& This prog will help in understanding ALV OOPS
*& EDIT on SAVE
*& Tool bar button addition
*&---------------------------------------------------------------------*
REPORT YKC_ALV_OOPS.
tables: MARA.
data: begin of it_tab occurs 0,
matnr like mara-matnr,
ersda like mara-ersda, "creation date
ernam like mara-ernam, "person created
pstat like mara-pstat, "maint stat
lvorm like mara-lvorm, "flg for deletion
mtart like mara-mtart, "mat type
meins like mara-meins, "uom
end of it_tab.
data: wa_it_tab like line of it_tab. "making work area
data: i_modified TYPE STANDARD TABLE OF mara,"For getting modified rows
w_modified TYPE mara.
CLASS lcl_events_d0100 DEFINITION DEFERRED.
DATA: event_receiver1 TYPE REF TO lcl_events_d0100,
i_selected_rows TYPE lvc_t_row, "Selected Rows
w_selected_rows TYPE lvc_s_row.
*---------------------------------------------------------------------*
* CLASS lcl_events_d0100 DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_events_d0100 DEFINITION.
PUBLIC SECTION.
METHODS
handle_hotspot_click
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING
e_row_id
e_column_id
es_row_no
sender.
*---code addition for ALV pushbuttons
*--for placing buttons
METHODS handle_toolbar_set
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING
e_object
e_interactive.
*---user command on clicking a button
METHODS handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING
e_ucomm.
ENDCLASS. "lcl_events_d0100 DEFINITION
TYPE-POOLS cndp.
DATA ok_code TYPE sy-ucomm.
*----------------------------------------------------------------------*
* FOR VARIANT
*----------------------------------------------------------------------*
DATA st_var TYPE disvariant .
DATA save TYPE c.
st_var-report = 'YKC_ALV_OOPS'.
save = 'A'.
*----------------------------------------------------------------------*
* FOR LAYOUT
*----------------------------------------------------------------------*
DATA loyo TYPE lvc_s_layo.
loyo-zebra = 'X'.
loyo-detailinit = 'X'.
loyo-info_fname = 'RED'.
*----------------------------------------------------------------------*
* FOR FIELD CATALOG
*----------------------------------------------------------------------*
DATA fcat TYPE lvc_t_fcat.
DATA wa_fcat LIKE LINE OF fcat.
*--Declaration for toolbar buttons
DATA : ty_toolbar TYPE stb_button.
DATA : e_object TYPE REF TO cl_alv_event_toolbar_set,
io_alv_toolbar TYPE REF TO cl_alv_event_toolbar_set.
*---custom container
DATA container TYPE REF TO cl_gui_custom_container.
DATA ref_grid TYPE REF TO cl_gui_alv_grid.
CREATE OBJECT container
EXPORTING
container_name = 'CONTAINER'."name of container in module pool
CREATE OBJECT ref_grid
EXPORTING
i_parent = container.
*---------------------------------------------------------------------*
* CLASS lcl_events_d0100 IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_events_d0100 IMPLEMENTATION.
*---method for hotspot
METHOD handle_hotspot_click.
DATA:ls_col_id TYPE lvc_s_col.
READ TABLE it_tab INTO wa_it_tab
INDEX e_row_id-index.
IF sy-subrc = 0.
CHECK ( wa_it_tab-matnr IS NOT INITIAL ).
CASE e_column_id-fieldname.
WHEN 'MATNR'.
leave program.
*---put your own logic as per requirement on hotspot click
WHEN OTHERS.
* do nothing
ENDCASE.
CALL METHOD ref_grid->set_current_cell_via_id
EXPORTING
is_row_id = e_row_id
is_column_id = ls_col_id.
ENDIF.
ENDMETHOD. "handle_hotspot_click
**---method for handling toolbar
METHOD handle_toolbar_set.
CLEAR ty_toolbar.
ty_toolbar-function = 'EDIT'. "name of btn to catch click
ty_toolbar-butn_type = 0.
ty_toolbar-text = 'EDIT'.
APPEND ty_toolbar TO e_object->mt_toolbar.
ENDMETHOD. "handle_toolbar_set
METHOD handle_user_command.
DATA: wr_data_changed TYPE REF TO cl_alv_changed_data_protocol.
DATA: lt_rows TYPE lvc_t_row,
lt_index TYPE lvc_s_row-index.
CASE e_ucomm.
WHEN 'EDIT'.
perform save_database.
CALL METHOD ref_GRID->REFRESH_TABLE_DISPLAY.
ENDCASE.
ENDMETHOD. "handle_user_command
ENDCLASS. "lcl_events_d0100 IMPLEMENTATION
START-OF-SELECTION.
PERFORM get_data.
PERFORM field_catalog.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* text : getting data into internal table
*----------------------------------------------------------------------*
form get_data .
select matnr ersda ernam pstat lvorm mtart meins
into table it_tab
from mara
where matnr GE '000000000000000001'.
endform. " get_data
*&---------------------------------------------------------------------*
*& Form field_catalog
*&---------------------------------------------------------------------*
* text :setting field cat
*----------------------------------------------------------------------*
form field_catalog .
REFRESH fcat.
DATA: lv_pos TYPE i.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-coltext = 'Material No'.
wa_fcat-col_pos = lv_pos.
wa_fcat-hotspot = 'X'.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'ERSDA'.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-col_pos = lv_pos.
wa_fcat-edit = 'X'.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-coltext = 'Person Created'.
wa_fcat-col_pos = lv_pos.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'PSTAT'.
wa_fcat-coltext = 'Maint Stat'.
wa_fcat-col_pos = lv_pos.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'LVORM'.
wa_fcat-coltext = 'Flag For Deletion'.
wa_fcat-col_pos = lv_pos.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'MTART'.
wa_fcat-coltext = 'Material Type'.
wa_fcat-col_pos = lv_pos.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
lv_pos = lv_pos + 1.
wa_fcat-fieldname = 'MEINS'.
wa_fcat-coltext = 'UOM'.
wa_fcat-col_pos = lv_pos.
wa_fcat-outputlen = 18.
APPEND wa_fcat TO fcat.
CLEAR wa_fcat.
CREATE OBJECT event_receiver1.
*---setting event handlers
SET HANDLER event_receiver1->handle_toolbar_set FOR ref_grid.
SET HANDLER event_receiver1->handle_user_command FOR ref_grid.
SET HANDLER event_receiver1->handle_hotspot_click FOR ref_grid.
*----------------------------------------------------------------------*
* ALV GRID DISPLAY
*----------------------------------------------------------------------*
CALL METHOD ref_grid->set_table_for_first_display
EXPORTING
is_variant = st_var
i_save = save
is_layout = loyo
CHANGING
it_outtab = it_tab[]
it_fieldcatalog = fcat.
CALL SCREEN 100.
endform. " field_catalog
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module STATUS_0100 output.
* CREATE OBJECT gr_events_d0100.
*
* SET HANDLER gr_events_d0100->double_click
*
* FOR ref_grid.
CALL METHOD ref_grid->register_edit_event
EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified.
SET PF-STATUS 'S100'.
SET TITLEBAR 'XXX'.
endmodule. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module exit INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module exit input.
CASE ok_code.
WHEN 'EXI' .
CLEAR ok_code.
LEAVE PROGRAM.
ENDCASE.
endmodule. " exit INPUT
*&---------------------------------------------------------------------*
*& Form SAVE_DATABASE
*&---------------------------------------------------------------------*
* text : saving into DDIC from internal table
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM SAVE_DATABASE .
*--- Getting the selected rows index
CALL METHOD ref_grid->get_selected_rows
IMPORTING et_index_rows = i_selected_rows.
*--- Through the index capturing the values of selected rows
LOOP AT i_selected_rows INTO w_selected_rows.
READ TABLE it_tab INTO wa_it_tab INDEX w_selected_rows-index.
IF sy-subrc EQ 0.
MOVE-CORRESPONDING wa_it_tab TO w_modified.
APPEND w_modified TO i_modified.
ENDIF.
ENDLOOP.
MODIFY mara FROM TABLE i_modified.
ENDFORM. " SAVE_DATABASE

Das könnte Ihnen auch gefallen