Friday, March 31, 2017

Sample Code : ALV using factory class








Sample Code




*&---------------------------------------------------------------------*
*& Report YTEST220_ALV
*&
*&---------------------------------------------------------------------*
*&
*& By Debesh
*&---------------------------------------------------------------------*

report ytest220_alv.

include ytest220_alv_top. “ At the end
include ytest220_alv_form.

start-of-selection.

select kunnr
land1
name1
name2
ort01
pstlz
regio
sortl
stras
telf1
telfx
xcpdk
adrnr
from kna1 into table gt_kna1 where kunnr in so_kunnr.

if sy-subrc is initial.

if p_r3 is not initial.

call screen 0100.

endif.

endif.















*&---------------------------------------------------------------------*
*& Include YTEST220_ALV_FORM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0100 output.
set pf-status '0100'.
set titlebar '0100'.

endmodule. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0100 input.

case sy-ucomm.
when 'BACK'.
refresh gt_kna1.
submit ytest220_alv via selection-screen.
when 'EXIT'.
leave program.
when others.

endcase.

endmodule. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Module populate_alv OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module populate_alv output.

data : lo_container type ref to cl_gui_custom_container,
lo_splitter type ref to cl_gui_splitter_container,
lo_container1 type ref to cl_gui_container,
lo_container2 type ref to cl_gui_container,
lo_header type ref to cl_dd_document,
lo_colref type ref to cl_salv_columns_table,
lo_col type ref to cl_salv_column,
lo_funclist type ref to cl_salv_functions_list,
lo_event type ref to cl_salv_events_table.


if lo_container is initial.
******************************************************Create container.
create object lo_container
exporting
container_name = 'CC_CONTAINER'
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

*********************************************************Split Container.
create object lo_splitter
exporting
parent = lo_container
rows = 2
columns = 1
exceptions
cntl_error = 1
cntl_system_error = 2
others = 3
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

************************************************************Get container.
************************************************************ HEADER

call method lo_splitter->get_container
exporting
row = 1
column = 1
receiving
container = lo_container1.


call method lo_splitter->set_row_height
exporting
id = 1
height = 10
exceptions
cntl_error = 1
cntl_system_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

create object lo_header.

call method lo_header->add_text
exporting
text = text-001
* sap_color = 'red'
* sap_fontsize = '5'
sap_emphasis = 'STRONG'
.

call method lo_header->display_document
exporting
parent = lo_container1
exceptions
html_display_error = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.


*************************************************************Get container.
************************************************************* Content
call method lo_splitter->get_container
exporting
row = 2
column = 1
receiving
container = lo_container2.
call method lo_splitter->set_row_height
exporting
id = 2
height = 80
exceptions
cntl_error = 1
cntl_system_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.


** Call Factory method to get ALV table reference

try.
call method cl_salv_table=>factory
exporting
list_display = if_salv_c_bool_sap=>false
r_container = lo_container2
importing
r_salv_table = gt_tabref
changing
t_table = gt_kna1.
catch cx_salv_msg .
endtry.

*********************************************************** SET COLUMN VISBILITY
lo_colref = gt_tabref->get_columns( ).
try.
lo_col ?= lo_colref->get_column( 'NAME2' ).
call method lo_col->set_visible
exporting
value = if_salv_c_bool_sap=>false.

catch cx_salv_not_found.

endtry.

********************************************************* To select multiple rows at a time

call method gt_tabref->get_selections
receiving
value = go_sel.
call method go_sel->set_selection_mode
exporting
value = if_salv_c_selection_mode=>row_column.

******************************************************** Get Functions
call method gt_tabref->get_functions
receiving
value = lo_funclist.
lo_funclist->set_all( abap_true ).

try.
call method lo_funclist->add_function
exporting
name = 'CC_DETAILS'
* ICON =
text = 'CC Details'
tooltip = 'Company Code Details'
position = if_salv_c_function_position=>right_of_salv_functions
.
catch cx_salv_existing .
catch cx_salv_wrong_call .
endtry.

call method gt_tabref->get_event
receiving
value = lo_event.
set handler lcl_event_handling=>handle_user_command for all instances.

******************************************************** Display Using alv table reference display table
call method gt_tabref->display.
else.
call method gt_tabref->refresh.
endif.

endmodule. " populate_alv OUTPUT
*&---------------------------------------------------------------------*
*& Form form_get_selected
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form form_get_selected .

data : ls_rows type int4.
data : ls_kna1 type ty_data.
data : lr_kunnr type range of kna1-kunnr,
lrs_kunnr like line of lr_kunnr.

call method gt_tabref->get_metadata.
* FREE go_sel.
call method gt_tabref->get_selections
receiving
value = go_sel.
gt_rows = go_sel->get_selected_rows( ).

loop at gt_rows into ls_rows.

read table gt_kna1 into ls_kna1 index ls_rows.
if sy-subrc is initial.
lrs_kunnr-sign = 'I'.
lrs_kunnr-option = 'EQ'.
lrs_kunnr-low = ls_kna1-kunnr.
append lrs_kunnr to lr_kunnr.
endif.
clear lrs_kunnr.
endloop.

if lr_kunnr is not initial.
select kunnr
bukrs
pernr
erdat
ernam
sperr
loevm
zuawa
busab
akont
from knb1 into table gt_knb1
where kunnr in lr_kunnr.

if sy-subrc is initial.
clear lr_kunnr.
call screen 200.
endif.

endif.


endform. " form_get_selected
*&---------------------------------------------------------------------*
*& Module STATUS_0200 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0200 output.
set pf-status '0200'.
set titlebar '0200'.
perform populate_alv2.
endmodule. " STATUS_0200 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0200 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0200 input.

case sy-ucomm.
when 'BACK'.
refresh gt_knb1.
* IF gt_tabref2 IS BOUND.
* FREE gt_tabref2.
* ENDIF.

leave list-processing.
set screen 100.
when 'EXIT'.
leave program.
when others.

endcase.
endmodule. " USER_COMMAND_0200 INPUT
*&---------------------------------------------------------------------*
*& Form populate_alv2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form populate_alv2 .

data : lo_containerr type ref to cl_gui_custom_container,
lo_splitter2 type ref to cl_gui_splitter_container,
lo_cont1 type ref to cl_gui_container,
lo_cont2 type ref to cl_gui_container,
lo_head2 type ref to cl_dd_document,
lo_colref2 type ref to cl_salv_columns_table,
lo_col2 type ref to cl_salv_column,
lo_funclist2 type ref to cl_salv_functions_list,
lo_event2 type ref to cl_salv_events_table.

if lo_containerr is initial.
******************************************************Create container.
create object lo_containerr
exporting
container_name = 'CC_CONTAINERR'
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

*********************************************************Split Container.
create object lo_splitter2
exporting
parent = lo_containerr
rows = 2
columns = 1
exceptions
cntl_error = 1
cntl_system_error = 2
others = 3
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

************************************************************Get container.
************************************************************ HEADER

call method lo_splitter2->get_container
exporting
row = 1
column = 1
receiving
container = lo_cont1.


call method lo_splitter2->set_row_height
exporting
id = 1
height = 10
exceptions
cntl_error = 1
cntl_system_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

create object lo_head2.

call method lo_head2->add_text
exporting
text = text-002
* sap_color = 'red'
* sap_fontsize = '5'
sap_emphasis = 'STRONG'
.

call method lo_head2->display_document
exporting
parent = lo_cont1
exceptions
html_display_error = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.


*************************************************************Get container.
************************************************************* Content
call method lo_splitter2->get_container
exporting
row = 2
column = 1
receiving
container = lo_cont2.
call method lo_splitter2->set_row_height
exporting
id = 2
height = 80
exceptions
cntl_error = 1
cntl_system_error = 2
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.


** Call Factory method to get ALV table reference
if gt_tabref2 is not initial.
gt_tabref2->refresh( ).
endif.

try.
call method cl_salv_table=>factory
exporting
list_display = if_salv_c_bool_sap=>false
r_container = lo_cont2
importing
r_salv_table = gt_tabref2
changing
t_table = gt_knb1.
catch cx_salv_msg .
endtry.

*********************************************************** SET COLUMN VISBILITY
lo_colref2 = gt_tabref2->get_columns( ).
try.
lo_col2 ?= lo_colref2->get_column( 'NAME2' ).
call method lo_col2->set_visible
exporting
value = if_salv_c_bool_sap=>false.

catch cx_salv_not_found.

endtry.

********************************************************* To select multiple rows at a time

call method gt_tabref2->get_selections
receiving
value = go_sel2.
call method go_sel2->set_selection_mode
exporting
value = if_salv_c_selection_mode=>row_column.

******************************************************** Get Functions
call method gt_tabref2->get_functions
receiving
value = lo_funclist2.
lo_funclist2->set_all( abap_true ).


******************************************************** Display Using alv table reference display table
call method gt_tabref2->refresh.
call method gt_tabref2->display.
else.
call method gt_tabref2->refresh.
endif.
endform. " populate_alv2

*&---------------------------------------------------------------------*
*& Include YTEST220_ALV_TOP
*&---------------------------------------------------------------------*
tables : kna1.


selection-screen begin of block b1 with frame.
select-options : so_kunnr for kna1-kunnr obligatory.
selection-screen end of block b1.


selection-screen begin of block b2 with frame.
parameters:p_r1 type c radiobutton group rg1 default 'X'.
parameters:p_r2 type c radiobutton group rg1 .
parameters:p_r3 type c radiobutton group rg1 .
selection-screen end of block b2 .

types: begin of ty_data,
kunnr type kunnr ,
land1 type land1_gp ,
name1 type name1_gp ,
name2 type name2_gp ,
ort01 type ort01_gp ,
pstlz type pstlz ,
regio type regio ,
sortl type sortl ,
stras type stras_gp ,
telf1 type telf1 ,
telfx type telfx ,
xcpdk type xcpdk ,
adrnr type adrnr ,
end of ty_data.

types: begin of ty_compdata,
kunnr type kunnr ,
bukrs type bukrs ,
pernr type pernr_d ,
erdat type erdat_rf ,
ernam type ernam_rf ,
sperr type sperb_b ,
loevm type loevm_b ,
zuawa type dzuawa ,
busab type busab ,
akont type akont ,
end of ty_compdata.

data : gt_kna1 type standard table of ty_data,
gt_knb1 type standard table of ty_compdata.
data : gt_tabref type ref to cl_salv_table,
gt_tabref2 type ref to cl_salv_table,
go_sel type ref to cl_salv_selections,
go_sel2 type ref to cl_salv_selections.
data: gt_rows type salv_t_row.

*----------------------------------------------------------------------*
* CLASS lcl_event_handling DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handling definition.
public section.
class-methods:
handle_user_command
for event user_command of cl_gui_alv_grid
importing e_ucomm.
private section.
endclass. "lcl_event_handling DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_event_handling IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handling implementation.

method handle_user_command.
case e_ucomm.

when 'CC_DETAILS'.
perform form_get_selected.
endcase.
endmethod. "handle_user_command
endclass. "lcl_event_handling IMPLEMENTATION





*&---------------------------------------------------------------------*
*& Include YTEST220_ALV_TOP
*&---------------------------------------------------------------------*
TABLES : kna1.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS : so_kunnr FOR kna1-kunnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.




TYPES: BEGIN OF ty_data,
kunnr TYPE kunnr ,
land1 TYPE land1_gp ,
name1 TYPE name1_gp ,
name2 TYPE name2_gp ,
ort01 TYPE ort01_gp ,
pstlz TYPE pstlz ,
regio TYPE regio ,
sortl TYPE sortl ,
stras TYPE stras_gp ,
telf1 TYPE telf1 ,
telfx TYPE telfx ,
xcpdk TYPE xcpdk ,
adrnr TYPE adrnr ,
END OF ty_data.

TYPES: BEGIN OF ty_compdata,
kunnr TYPE kunnr ,
bukrs TYPE bukrs ,
pernr TYPE pernr_d ,
erdat TYPE erdat_rf ,
ernam TYPE ernam_rf ,
sperr TYPE sperb_b ,
loevm TYPE loevm_b ,
zuawa TYPE dzuawa ,
busab TYPE busab ,
akont TYPE akont ,
END OF ty_compdata.

DATA : gt_kna1 TYPE TABLE OF ty_data,
gt_knb1 TYPE STANDARD TABLE OF ty_compdata.
DATA : gt_tabref TYPE REF TO cl_salv_table,
gt_tabref2 TYPE REF TO cl_salv_table,
go_sel TYPE REF TO cl_salv_selections,
go_sel2 TYPE REF TO cl_salv_selections,
lo_container TYPE REF TO cl_gui_custom_container,
lo_containerr TYPE REF TO cl_gui_custom_container.
DATA: gt_rows TYPE salv_t_row.



*----------------------------------------------------------------------*
* CLASS lcl_event_handling DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handling DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
PRIVATE SECTION.
ENDCLASS. "lcl_event_handling DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_event_handling IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handling IMPLEMENTATION.

METHOD handle_user_command.
CASE e_ucomm.

WHEN 'CC_DETAILS'.
* PERFORM form_get_selected.
ENDCASE.
ENDMETHOD. "handle_user_command
ENDCLASS. "lcl_event_handling IMPLEMENTATION



























Sample Code : Selection Screen













Sample code





REPORT ztrm_login.

DATA : c_set_true TYPE c VALUE '1'.
DATA : c_set_false TYPE c VALUE '0'.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS : r_g1 RADIOBUTTON GROUP g1 DEFAULT 'X' USER-COMMAND chk.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS : p_userid TYPE ztrm_login-user_no MODIF ID log OBLIGATORY.
PARAMETERS : p_pwd TYPE ztrm_login-password MODIF ID log .
SELECTION-SCREEN : BEGIN OF LINE,
PUSHBUTTON 31(10) push1 USER-COMMAND push MODIF ID log.
SELECTION-SCREEN : END OF LINE.
SELECTION-SCREEN END OF BLOCK b2 .

PARAMETERS : r_g2 RADIOBUTTON GROUP g1 .

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-003.
PARAMETERS: p_fname TYPE ztrm_login-first_name MODIF ID reg,
p_mname TYPE ztrm_login-middle_name MODIF ID reg,
p_lname TYPE ztrm_login-last_name MODIF ID reg,
p_mob TYPE ztrm_login-mobile MODIF ID reg,
p_pan TYPE ztrm_login-pan_card MODIF ID reg,
p_adh TYPE ztrm_login-adhar_card MODIF ID reg,
p_dob TYPE ztrm_login-date_of_birth MODIF ID reg,
p_mail TYPE ztrm_login-zemail MODIF ID reg,
p_utype TYPE ztrm_login-zuser_ty MODIF ID reg,
p_pwd2 TYPE ztrm_login-password MODIF ID reg.

SELECTION-SCREEN: BEGIN OF LINE,
PUSHBUTTON 31(10) push2 USER-COMMAND push MODIF ID reg.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b3.

SELECTION-SCREEN END OF BLOCK b1.

INITIALIZATION.
push1 = 'LOG-IN'.
push2 = 'REGISTER'.

AT SELECTION-SCREEN OUTPUT.

IF r_g1 = 'X'.
LOOP AT SCREEN.
IF screen-group1 = 'REG'.

screen-required = c_set_false.
screen-invisible = c_set_true.
screen-active = c_set_false.
screen-input = c_set_false.
MODIFY SCREEN.
CONTINUE.
ELSEIF screen-group1 = 'LOG'.
screen-required = '2'.
screen-invisible = c_set_false.
screen-active = c_set_true.
screen-input = c_set_true.
MODIFY SCREEN.
CONTINUE.

ENDIF.
ENDLOOP.
ELSEIF r_g2 = 'X' .
LOOP AT SCREEN.
IF screen-group1 = 'LOG'.
screen-required = c_set_false.
screen-invisible = c_set_true.
screen-active = c_set_false.
screen-input = c_set_false.
MODIFY SCREEN.
CONTINUE.
ELSEIF screen-group1 = 'REG'.
IF screen-name = 'P_FNAME' OR screen-name = 'P_LNAME'
OR screen-name = 'P_MOB' OR screen-name = 'P_PAN'
OR screen-name = 'P_ADH' OR screen-name = 'P_PWD2'.
screen-required = '2'.
ENDIF.
screen-invisible = c_set_false.
screen-active = c_set_true.
screen-input = c_set_true.
MODIFY SCREEN.
CONTINUE.

ENDIF.
ENDLOOP.
ENDIF.

Guess the output : OO ABAP Multiple Inheritance Sample Code



Multiple Inheritance Example






*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP6
*&
*&---------------------------------------------------------------------*
*& By Debesh
*&
*&---------------------------------------------------------------------*

REPORT ytest_ooabap6.

*----------------------------------------------------------------------*
* INTERFACE country
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE country.
METHODS set_country.
DATA : country TYPE char10.
ENDINTERFACE. "country
*----------------------------------------------------------------------*
* INTERFACE state
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE state.
METHODS set_state.
DATA : state TYPE char10.
ENDINTERFACE. "state

*----------------------------------------------------------------------*
* CLASS city_bhubaneswar DEFINITION
*----------------------------------------------------------------------*
CLASS city_bhubaneswar DEFINITION.
PUBLIC SECTION.
INTERFACES: country, state.
ENDCLASS. "city_bhubaneswar DEFINITION

*----------------------------------------------------------------------*
* CLASS city_cuttack DEFINITION
*----------------------------------------------------------------------*

CLASS city_bangalore DEFINITION.
PUBLIC SECTION.
INTERFACES: country , state.
ENDCLASS. "city_cuttack DEFINITION

*----------------------------------------------------------------------*
* CLASS city_bhubaneswar IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS city_bhubaneswar IMPLEMENTATION.
METHOD country~set_country.
country~country = 'INDIA'.
ENDMETHOD. "country~set_country
METHOD state~set_state.
state~state = 'ODISHA'.
ENDMETHOD. "state~set_state
ENDCLASS. "city_bhubaneswar IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS city_cuttack IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS city_bangalore IMPLEMENTATION.
METHOD country~set_country.
country~country = 'INDIA'.
ENDMETHOD. "country~set_country
METHOD state~set_state.
state~state = 'KARNATAKA'.
ENDMETHOD. "state~set_state
ENDCLASS. "city_cuttack IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS smart_city DEFINITION
*----------------------------------------------------------------------*
CLASS smart_city DEFINITION.
PUBLIC SECTION .
METHODS display_smart_city.
ENDCLASS. "smart_city DEFINITION

*----------------------------------------------------------------------*
* CLASS smart_city IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS smart_city IMPLEMENTATION.
METHOD display_smart_city.
DATA : lo_bhubaneswar TYPE REF TO city_bhubaneswar.
DATA : lo_bangalore TYPE REF TO city_bangalore.

CREATE OBJECT: lo_bhubaneswar, lo_bangalore.
lo_bhubaneswar->country~set_country( ).
lo_bhubaneswar->state~set_state( ).

lo_bangalore->country~set_country( ).
lo_bangalore->state~set_state( ).

NEW-LINE.
WRITE : 'Bhubaneswar'.
ULINE.
WRITE : 'Country ->'.
WRITE : lo_bhubaneswar->country~country.
NEW-LINE.
WRITE : 'state ->'.
WRITE : lo_bhubaneswar->state~state.
uline.
NEW-LINE.
WRITE : 'Bangalore'.
ULINE.
WRITE : 'Country ->'.
WRITE : lo_bangalore->country~country.
NEW-LINE.
WRITE : 'state ->'.
WRITE : lo_bangalore->state~state.
ENDMETHOD. "display_smart_city
ENDCLASS. "smart_city IMPLEMENTATION

START-OF-SELECTION.
DATA : lo_smart_city TYPE REF TO smart_city.
CREATE OBJECT lo_smart_city.
lo_smart_city->display_smart_city( ).







OUTPUT
Test Object


Bhubaneswar
Country -> INDIA
state -> ODISHA




Bangalore
Country -> INDIA
state -> KARNATAKA

Guess the output : OO ABAP CASTING Sample Code



Casting Example


*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP5
*&
*&---------------------------------------------------------------------*
*& Upcast/Widening Cast
*& Dowbcast/Narrow cast
*&---------------------------------------------------------------------*

REPORT ytest_ooabap5.

*----------------------------------------------------------------------*
* CLASS property DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property DEFINITION.
PUBLIC SECTION.
METHODS : get_property IMPORTING business_partner TYPE char10 RETURNING value(address) TYPE string ,
set_property IMPORTING business_partner TYPE string address TYPE string,
constructor IMPORTING business_partner TYPE string address TYPE string,
display .
PROTECTED SECTION.
DATA : bp TYPE char10, addr TYPE string.
ENDCLASS. "property DEFINITION

*----------------------------------------------------------------------*
* CLASS property_transfer DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property_transfer DEFINITION INHERITING FROM property.
PUBLIC SECTION.
METHODS : change_property_owner CHANGING value(business_partner) TYPE char10,
display REDEFINITION.
ENDCLASS. "property_transfer DEFINITION

*----------------------------------------------------------------------*
* CLASS property IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property IMPLEMENTATION.
METHOD constructor.
bp = business_partner.
addr = address.
ENDMETHOD. "constructor
METHOD get_property.
address = addr.
ENDMETHOD. "get_property
METHOD set_property.
addr = address.
ENDMETHOD. "set_property
METHOD display.
ULINE.
WRITE : 'Parent'.
ULINE.
ENDMETHOD. "display
ENDCLASS. "property IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS property_transfer IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS property_transfer IMPLEMENTATION.
METHOD change_property_owner.
bp = business_partner.
ENDMETHOD. "change_property_owner
METHOD display.
ULINE.
WRITE : 'Child'.
ULINE.
ENDMETHOD. "display
ENDCLASS. "property_transfer IMPLEMENTATION

START-OF-SELECTION.
DATA : lo_property TYPE REF TO property,
lo_property_transfer TYPE REF TO property_transfer.

DATA : lv_bp TYPE char10 VALUE '153',
lv_error TYPE REF TO cx_root.


CREATE OBJECT lo_property EXPORTING business_partner = '151' address = 'HSR' .
CREATE OBJECT lo_property_transfer EXPORTING business_partner = '152' address = 'BTM' .

* lo_property = lo_property_transfer.
* lo_property->change_property_owner( business_partner = '153' ). " Syntax error : Object does not exist

* lo_property_transfer = lo_property. "Syntax Error : Type conversion
lo_property = lo_property_transfer.

TRY.
lo_property_transfer ?= lo_property.
lo_property_transfer->change_property_owner( CHANGING business_partner = lv_bp )."'CX_SY_MOVE_CAST_ERROR'
CATCH cx_sy_move_cast_error INTO lv_error.
WRITE : 'CAST ERROR'.
ENDTRY.

lo_property->display( ).
lo_property_transfer->display( ).





OUTPUT



Test Program


Child

Child

Guess the output : OO ABAP Singleton Class Sample Code



Singleton Class Example


*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP4
*&
*&---------------------------------------------------------------------*
*&class <name> definition create private.
*&class-data instance type ref to <name> in private section
*&Class_method changing reference type ref to <name> in Public section
*&
*& By Debesh
*&---------------------------------------------------------------------*

REPORT ytest_ooabap4.

DATA : it_mara TYPE TABLE OF mara,
wa_mara TYPE mara.
*----------------------------------------------------------------------*
* CLASS CL_SINGLETON DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_singleton DEFINITION CREATE PRIVATE. "create a private class
PUBLIC SECTION.
METHODS: get_mara. "actual method get mara
CLASS-METHODS: instantiate CHANGING lr_inst TYPE REF TO cl_singleton . "create a static method
PRIVATE SECTION.
CLASS-DATA: lr_inst TYPE REF TO cl_singleton. "private variable
ENDCLASS. "CL_SINGLETON DEFINITION

*----------------------------------------------------------------------*
* CLASS CL_SINGLETON IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_singleton IMPLEMENTATION.
METHOD get_mara.
SELECT * FROM mara INTO TABLE it_mara UP TO 5 ROWS.
LOOP AT it_mara INTO wa_mara.
WRITE:/ wa_mara-matnr, wa_mara-mtart, wa_mara-meins, wa_mara-matkl.
ENDLOOP.
ENDMETHOD. "GET_MARA
METHOD instantiate.
IF lr_inst IS INITIAL.
CREATE OBJECT lr_inst. "create object
ELSE.
ULINE.
WRITE : 'Already instantiated'.
ENDIF.
ENDMETHOD. "INSTANTIATE

ENDCLASS. "CL_SINGLETON IMPLEMENTATION

START-OF-SELECTION.

DATA : lo_class TYPE REF TO cl_singleton.
*CREATE OBJECT lo_class."syntax error

cl_singleton=>instantiate( CHANGING lr_inst = lo_class ). "get instance of class
*CREATE OBJECT LO_CLASS.
lo_class->get_mara( ). "get mara data

cl_singleton=>instantiate( CHANGING lr_inst = lo_class ).





OUTPUT



Test Program




38 DATA KWH
41 DATA KWH
43 DATA KWH
46 DATA KWH NUISU
49 DATA KWH


Already instantiated

Guess the output : OO ABAP Friends Concept Sample Code



Friends Concept Example


*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP3
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT YTEST_OOABAP3.

CLASS Test_Employee DEFINITION DEFERRED.

*----------------------------------------------------------------------*
* CLASS test_organization DEFINITION
*----------------------------------------------------------------------*
CLASS test_organization DEFINITION FRIENDS Test_Employee.

PUBLIC SECTION.

DATA : stock_price TYPE char10 VALUE 100.

METHODS display_stock_price.

PRIVATE SECTION.

DATA : avg_ctc TYPE char10 VALUE 200.

METHODS display_avg_ctc.

ENDCLASS. "test_organization DEFINITION

*----------------------------------------------------------------------*
* CLASS test_organization IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS test_organization IMPLEMENTATION.

METHOD display_stock_price.

WRITE / stock_price.

SKIP.

ENDMETHOD. "display_stock_price

METHOD display_avg_ctc.

WRITE / avg_ctc.

SKIP.

ENDMETHOD. "display_avg_ctc

ENDCLASS. "test_organization IMPLEMENTATION


*----------------------------------------------------------------------*
* CLASS Test_Employee DEFINITION
*----------------------------------------------------------------------*
CLASS Test_Employee DEFINITION INHERITING FROM test_organization.

PUBLIC SECTION.

METHODS : display_emp.

ENDCLASS. "Test_Employee DEFINITION


*----------------------------------------------------------------------*
* CLASS Test_Employee IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS Test_Employee IMPLEMENTATION.

METHOD display_emp.

DATA ref_empl TYPE REF TO test_organization.

CREATE OBJECT ref_empl.

CALL METHOD ref_empl->display_stock_price.
CALL METHOD ref_empl->display_avg_ctc.

ENDMETHOD. "display_emp

ENDCLASS. "Test_Employee IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASStest_Shareholder DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS test_Shareholder DEFINITION INHERITING FROM test_organization.

PUBLIC SECTION.

METHODS display_shr.

ENDCLASS. "abc_shareholder DEFINITION

*----------------------------------------------------------------------*
* CLASStest_Shareholder IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS test_Shareholder IMPLEMENTATION.

METHOD display_shr.

DATA ref_shr TYPE REF TO test_organization.

CREATE OBJECT ref_shr.

CALL METHOD ref_shr->display_stock_price.
* CALL METHOD ref_shr->display_avg_ctc. " private method syntax error.

ENDMETHOD. "display_emp

ENDCLASS. "abc_shareholder IMPLEMENTATION

START-OF-SELECTION.

DATA : ref_emp TYPE REF TO Test_Employee,
ref_shr TYPE REF TO test_Shareholder.

CREATE OBJECT ref_emp.
CREATE OBJECT ref_shr.

CALL METHOD ref_emp->display_emp.

CALL METHOD ref_shr->display_shr.

OUTPUT
Test Program
      100
      200

      100

Guess the output : OO ABAP Constructor Sequence Sample Code



Constructor Sequence Example


*&---------------------------------------------------------------------*
*& Report YTEST2_OOABAP
*&
*&---------------------------------------------------------------------*
*& Developed By Debesh
*&
*&---------------------------------------------------------------------*

REPORT ytest2_ooabap.

*----------------------------------------------------------------------*
* CLASS First_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS first_class DEFINITION.
PUBLIC SECTION.
METHODS : constructor.
CLASS-METHODS:class_constructor.
ENDCLASS. "First_class DEFINITION
*----------------------------------------------------------------------*
* CLASS Second_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS second_class DEFINITION INHERITING FROM first_class.
PUBLIC SECTION.
METHODS : constructor.
CLASS-METHODS:class_constructor.
ENDCLASS. "Second_class DEFINITION
*----------------------------------------------------------------------*
* CLASS first_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS first_class IMPLEMENTATION.
METHOD constructor.
NEW-LINE.
WRITE : 'This is FIRST_CLASS CONSTRUCTOR'.
ENDMETHOD. "constructor
METHOD class_constructor.
NEW-LINE.
WRITE : 'This is FIRST_CLASS CLASS CONSTRUCTOR'.
ENDMETHOD. "class_constructor
ENDCLASS. "first_class IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS first_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS second_class IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
NEW-LINE.
WRITE : 'This is SECOND_CLASS CONSTRUCTOR'.
ENDMETHOD. "constructor
METHOD class_constructor.
NEW-LINE.
WRITE : 'This is SECOND_CLASS CLASS CONSTRUCTOR'.
ENDMETHOD. "class_constructor
ENDCLASS. "first_class IMPLEMENTATION

START-OF-SELECTION.

DATA : lo_first TYPE REF TO first_class.
DATA : lo_second TYPE REF TO second_class.
CREATE OBJECT lo_second.






OUTPUT


OO ABAP Test Program


This is FIRST_CLASS CLASS CONSTRUCTOR

This is SECOND_CLASS CLASS CONSTRUCTOR

This is FIRST_CLASS CONSTRUCTOR

This is SECOND_CLASS CONSTRUCTOR

Guess the output : OO ABAP Constructor Sample Code



Constructor Example


*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP
*&
*&---------------------------------------------------------------------*
*& Developed By Debesh
*&
*&---------------------------------------------------------------------*

REPORT ytest_ooabap.

*----------------------------------------------------------------------*
* CLASS First_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS first_class DEFINITION.
PUBLIC SECTION.
METHODS : constructor.
CLASS-METHODS:class_constructor.
ENDCLASS. "First_class DEFINITION
*----------------------------------------------------------------------*
* CLASS Second_class DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS second_class DEFINITION.
PUBLIC SECTION.
METHODS : constructor.
CLASS-METHODS:class_constructor.
ENDCLASS. "Second_class DEFINITION
*----------------------------------------------------------------------*
* CLASS first_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS first_class IMPLEMENTATION.
METHOD constructor.
NEW-LINE.
WRITE : 'This is FIRST_CLASS CONSTRUCTOR'.
ENDMETHOD. "constructor
METHOD class_constructor.
NEW-LINE.
WRITE : 'This is FIRST_CLASS CLASS CONSTRUCTOR'.
ENDMETHOD. "class_constructor
ENDCLASS. "first_class IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS first_class IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS second_class IMPLEMENTATION.
METHOD constructor.
NEW-LINE.
WRITE : 'This is SECOND_CLASS CONSTRUCTOR'.
ENDMETHOD. "constructor
METHOD class_constructor.
NEW-LINE.
WRITE : 'This is SECOND_CLASS CLASS CONSTRUCTOR'.
ENDMETHOD. "class_constructor
ENDCLASS. "first_class IMPLEMENTATION

START-OF-SELECTION.

DATA : lo_first TYPE REF TO first_class.
DATA : lo_second TYPE REF TO second_class.

CREATE OBJECT lo_first.
CREATE OBJECT lo_second.













Output


OO ABAP Test Program

This is FIRST_CLASS CLASS CONSTRUCTOR


This is FIRST_CLASS CONSTRUCTOR


This is SECOND_CLASS CLASS CONSTRUCTOR


This is SECOND_CLASS CONSTRUCTOR