Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

OOABAP

Former Member
0 Kudos

Can anyone help me with some examples of OOABAP

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Please check this online document (starting page 1291).

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf

Also check this links as well.

http://help.sap.com/saphelp_nw2004s/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

http://www.futureobjects.de/content/intro_oo_e.html

http://www.sap-img.com/abap/business-add-in-you-need-to-understand-abap-oo-interface-concept.htm

/people/ravikumar.allampallam/blog/2005/02/11/abap-oo-in-action

check the below links lot of info and examples r there

http://www.sapgenie.com/abap/OO/index.htm

http://www.geocities.com/victorav15/sapr3/abap_ood.html

http://www.brabandt.de/html/abap_oo.html

Check this cool weblog:

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://www.allsaplinks.com/

http://www.sap-img.com/

http://www.sapgenie.com/

http://help.sap.com

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://www.sapgenie.com/abap/controls/index.htm

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

these links

http://help.sap.com/saphelp_47x200/helpdata/en/ce/b518b6513611d194a50000e8353423/content.htm

For funtion module to class

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm

for classes

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

for methods

http://help.sap.com/saphelp_47x200/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm

for inheritance

http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm

for interfaces

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm

Hope this resolves your query.

Reward all the helpful answers.

Kishi.

5 REPLIES 5

0 Kudos

HI,

GOto transaction ABAPDOCU and there check out the section ABAP Objects.

For example:

REPORT demo_abap_objects_methods NO STANDARD PAGE HEADING.

************************************************************************

  • Global Selection Screens

************************************************************************

SELECTION-SCREEN BEGIN OF: SCREEN 100 TITLE tit1, LINE.

PARAMETERS members TYPE i DEFAULT 10.

SELECTION-SCREEN END OF: LINE, SCREEN 100.

*----


SELECTION-SCREEN BEGIN OF: SCREEN 200 TITLE tit2.

PARAMETERS: drive RADIOBUTTON GROUP actn,

stop RADIOBUTTON GROUP actn,

gearup RADIOBUTTON GROUP actn,

geardown RADIOBUTTON GROUP actn.

SELECTION-SCREEN END OF: SCREEN 200.

************************************************************************

  • Class Definitions

************************************************************************

CLASS: c_biker DEFINITION DEFERRED,

c_bicycle DEFINITION DEFERRED.

*----


CLASS c_team DEFINITION.

PUBLIC SECTION.

TYPES: biker_ref TYPE REF TO c_biker,

biker_ref_tab TYPE STANDARD TABLE OF biker_ref

WITH DEFAULT KEY,

BEGIN OF status_line_type,

flag(1) TYPE c,

text1(5) TYPE c,

id TYPE i,

text2(7) TYPE c,

text3(6) TYPE c,

gear TYPE i,

text4(7) TYPE c,

speed TYPE i,

END OF status_line_type.

CLASS-METHODS: class_constructor.

METHODS: constructor,

create_team,

selection,

execution.

PRIVATE SECTION.

CLASS-DATA: team_members TYPE i,

counter TYPE i.

DATA: id TYPE i,

status_line TYPE status_line_type,

status_list TYPE SORTED TABLE OF status_line_type

WITH UNIQUE KEY id,

biker_tab TYPE biker_ref_tab,

biker_selection LIKE biker_tab,

biker LIKE LINE OF biker_tab.

METHODS: write_list.

ENDCLASS.

*----


CLASS c_biker DEFINITION.

PUBLIC SECTION.

METHODS: constructor IMPORTING team_id TYPE i members TYPE i,

select_action,

status_line EXPORTING line TYPE c_team=>status_line_type.

PRIVATE SECTION.

CLASS-DATA counter TYPE i.

DATA: id TYPE i,

bike TYPE REF TO c_bicycle,

gear_status TYPE i VALUE 1,

speed_status TYPE i VALUE 0.

METHODS biker_action IMPORTING action TYPE i.

ENDCLASS.

*----


CLASS c_bicycle DEFINITION.

PUBLIC SECTION.

METHODS: drive EXPORTING velocity TYPE i,

stop EXPORTING velocity TYPE i,

change_gear IMPORTING change TYPE i

RETURNING value(gear) TYPE i

EXCEPTIONS gear_min gear_max.

PRIVATE SECTION.

DATA: speed TYPE i,

gear TYPE i VALUE 1.

CONSTANTS: max_gear TYPE i VALUE 18,

min_gear TYPE i VALUE 1.

ENDCLASS.

************************************************************************

  • Class Implementations

************************************************************************

CLASS c_team IMPLEMENTATION.

METHOD class_constructor.

tit1 = 'Team members ?'.

CALL SELECTION-SCREEN 100 STARTING AT 5 3.

IF sy-subrc NE 0.

LEAVE PROGRAM.

ELSE.

team_members = members.

ENDIF.

ENDMETHOD.

METHOD constructor.

counter = counter + 1.

id = counter.

ENDMETHOD.

METHOD create_team.

DO team_members TIMES.

CREATE OBJECT biker EXPORTING team_id = id members = team_members.

APPEND biker TO biker_tab.

CALL METHOD biker->status_line IMPORTING line = status_line.

APPEND status_line TO status_list.

ENDDO.

ENDMETHOD.

METHOD selection.

CLEAR biker_selection.

DO.

READ LINE sy-index.

IF sy-subrc <> 0. EXIT. ENDIF.

IF sy-lisel+0(1) = 'X'.

READ TABLE biker_tab INTO biker INDEX sy-index.

APPEND biker TO biker_selection.

ENDIF.

ENDDO.

CALL METHOD write_list.

ENDMETHOD.

METHOD execution.

CHECK NOT biker_selection IS INITIAL.

LOOP AT biker_selection INTO biker.

CALL METHOD biker->select_action.

CALL METHOD biker->status_line IMPORTING line = status_line.

MODIFY TABLE status_list FROM status_line.

ENDLOOP.

CALL METHOD write_list.

ENDMETHOD.

METHOD write_list.

SET TITLEBAR 'TIT'.

sy-lsind = 0.

SKIP TO LINE 1.

POSITION 1.

LOOP AT status_list INTO status_line.

WRITE: / status_line-flag AS CHECKBOX,

status_line-text1,

status_line-id,

status_line-text2,

status_line-text3,

status_line-gear,

status_line-text4,

status_line-speed.

ENDLOOP.

ENDMETHOD.

ENDCLASS.

*----


CLASS c_biker IMPLEMENTATION.

METHOD constructor.

counter = counter + 1.

id = counter - members * ( team_id - 1 ).

CREATE OBJECT bike.

ENDMETHOD.

METHOD select_action.

DATA activity TYPE i.

tit2 = 'Select action for BIKE'.

tit2+24(3) = id.

CALL SELECTION-SCREEN 200 STARTING AT 5 15.

CHECK NOT sy-subrc GT 0.

IF gearup = 'X' OR geardown = 'X'.

IF gearup = 'X'.

activity = 1.

ELSEIF geardown = 'X'.

activity = -1.

ENDIF.

ELSEIF drive = 'X'.

activity = 2.

ELSEIF stop = 'X'.

activity = 3.

ENDIF.

CALL METHOD biker_action( activity ).

ENDMETHOD.

METHOD biker_action.

CASE action.

WHEN -1 OR 1.

CALL METHOD bike->change_gear

EXPORTING change = action

RECEIVING gear = gear_status

EXCEPTIONS gear_max = 1

gear_min = 2.

CASE sy-subrc.

WHEN 1.

MESSAGE i888(SABAPDOCU) WITH 'BIKE' id

' is already at maximal gear!'.

WHEN 2.

MESSAGE i888(SABAPDOCU) WITH 'BIKE' id

' is already at minimal gear!'.

ENDCASE.

WHEN 2.

CALL METHOD bike->drive IMPORTING velocity = speed_status.

WHEN 3.

CALL METHOD bike->stop IMPORTING velocity = speed_status.

ENDCASE.

ENDMETHOD.

METHOD status_line.

line-flag = space.

line-text1 = 'Biker'.

line-id = id.

line-text2 = 'Status:'.

line-text3 = 'Gear = '.

line-gear = gear_status.

line-text4 = 'Speed = '.

line-speed = speed_status.

ENDMETHOD.

ENDCLASS.

*----


CLASS c_bicycle IMPLEMENTATION.

METHOD drive.

speed = speed + gear * 10.

velocity = speed.

ENDMETHOD.

METHOD stop.

speed = 0.

velocity = speed.

ENDMETHOD.

METHOD change_gear.

gear = me->gear.

gear = gear + change.

IF gear GT max_gear.

gear = max_gear.

RAISE gear_max.

ELSEIF gear LT min_gear.

gear = min_gear.

RAISE gear_min.

ENDIF.

me->gear = gear.

ENDMETHOD.

ENDCLASS.

************************************************************************

  • Global Program Data

************************************************************************

TYPES team TYPE REF TO c_team.

DATA: team_blue TYPE team,

team_green TYPE team,

team_red TYPE team.

DATA color(5) TYPE c.

************************************************************************

  • Program events

************************************************************************

START-OF-SELECTION.

CREATE OBJECT: team_blue,

team_green,

team_red.

CALL METHOD: team_blue->create_team,

team_green->create_team,

team_red->create_team.

SET PF-STATUS 'TEAMLIST'.

WRITE ' Select a team! ' COLOR = 2.

*----


AT USER-COMMAND.

CASE sy-ucomm.

WHEN 'TEAM_BLUE'.

color = 'BLUE '.

FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.

CALL METHOD team_blue->selection.

WHEN 'TEAM_GREEN'.

color = 'GREEN'.

FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.

CALL METHOD team_green->selection.

WHEN 'TEAM_RED'.

color = 'RED '.

FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.

CALL METHOD team_red->selection.

WHEN 'EXECUTION'.

CASE color.

WHEN 'BLUE '.

FORMAT COLOR = 1 INTENSIFIED ON INVERSE ON.

CALL METHOD team_blue->selection.

CALL METHOD team_blue->execution.

WHEN 'GREEN'.

FORMAT COLOR = 5 INTENSIFIED ON INVERSE ON.

CALL METHOD team_green->selection.

CALL METHOD team_green->execution.

WHEN 'RED '.

FORMAT COLOR = 6 INTENSIFIED ON INVERSE ON.

CALL METHOD team_red->selection.

CALL METHOD team_red->execution.

ENDCASE.

ENDCASE.

************************************************************************

Regards,

Sesh

.

Former Member
0 Kudos

Hi,

Please check this online document (starting page 1291).

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf

Also check this links as well.

http://help.sap.com/saphelp_nw2004s/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

http://www.futureobjects.de/content/intro_oo_e.html

http://www.sap-img.com/abap/business-add-in-you-need-to-understand-abap-oo-interface-concept.htm

/people/ravikumar.allampallam/blog/2005/02/11/abap-oo-in-action

check the below links lot of info and examples r there

http://www.sapgenie.com/abap/OO/index.htm

http://www.geocities.com/victorav15/sapr3/abap_ood.html

http://www.brabandt.de/html/abap_oo.html

Check this cool weblog:

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://www.allsaplinks.com/

http://www.sap-img.com/

http://www.sapgenie.com/

http://help.sap.com

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://www.sapgenie.com/abap/controls/index.htm

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

these links

http://help.sap.com/saphelp_47x200/helpdata/en/ce/b518b6513611d194a50000e8353423/content.htm

For funtion module to class

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm

for classes

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

for methods

http://help.sap.com/saphelp_47x200/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm

for inheritance

http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm

for interfaces

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm

Hope this resolves your query.

Reward all the helpful answers.

Kishi.

Former Member
0 Kudos

Hi,

Give me your mail id.

Thanks,

Anitha

Former Member
0 Kudos

hi,

This example is a step by step example, moving from a simple class to more sophisticated classes,

and covers inheritance, interfaces and events.

Simple class

Inheritance and ploymorphism

Interfaces

Events

1. Simple class

This example shows how to create a simple employee class. The constructor method is used to initialize number and name of thje employee when the object is created. A display_employee method can be called to show the attributes of the employee, and CLASS-METHOD dosplay_no_of_employees can be called to show the total number of employees (Number of instances of the employee class).

REPORT zbc404_hf_events_1.

*********************************************************************

  • L C L _ E M P L O Y E E

*********************************************************************

*---- LCL Employee - Definition

CLASS lcl_employee DEFINITION.

PUBLIC SECTION.

*----

-


  • The public section is accesible from outside

*----

-


TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

END OF t_employee.

METHODS:

constructor

IMPORTING im_employee_no TYPE i

im_employee_name TYPE string,

display_employee.

  • Class methods are global for all instances

CLASS-METHODS: display_no_of_employees.

PROTECTED SECTION.

*----

-


  • The protecetd section is accesible from the class and its subclasses

*----

-


  • Class data are global for all instances

CLASS-DATA: g_no_of_employees TYPE i.

PRIVATE SECTION.

*----

-


  • The private section is only accesible from within the classs

*----

-


DATA: g_employee TYPE t_employee.

ENDCLASS.

*--- LCL Employee - Implementation

CLASS lcl_employee IMPLEMENTATION.

METHOD constructor.

g_employee-no = im_employee_no.

g_employee-name = im_employee_name.

g_no_of_employees = g_no_of_employees + 1.

ENDMETHOD.

METHOD display_employee.

WRITE:/ 'Employee', g_employee-no, g_employee-name.

ENDMETHOD.

METHOD display_no_of_employees.

WRITE: / 'Number of employees is:', g_no_of_employees.

ENDMETHOD.

ENDCLASS.

************************************************************************

  • R E P O R T

*********************************************************************

DATA: g_employee1 TYPE REF TO lcl_employee,

g_employee2 TYPE REF TO lcl_employee.

START-OF-SELECTION.

CREATE OBJECT g_employee1

EXPORTING im_employee_no = 1

im_employee_name = 'John Jones'.

CREATE OBJECT g_employee2

EXPORTING im_employee_no = 2

im_employee_name = 'Sally Summer'.

CALL METHOD g_employee1->display_employee.

CALL METHOD g_employee2->display_employee.

CALL METHOD g_employee2->display_no_of_employees.

2. Inheritance and polymorphism

This example uses a superclass lcl_company_employees and two subclasses lcl_bluecollar_employee and lcl_whitecollar_employee to add employees to a list and then display a list of employees and there wages. The wages are calcukated in the method add_employee, but as the wages are calculated differently for blue collar employees and white collar emplyees, the superclass method add_employee is redeifined in the subclasses.

Principles:

Create super class LCL_CompanyEmployees.

The class has the methods:

Constructor

Add_Employee - Adds a new employee to the list of employees

Display_Employee_List - Displays all employees and there wage

Display_no_of_employees - Displays total number of employees

Note the use of CLASS-DATA to keep the list of employees and number of employees the same from instance to instance.

Create subclasses lcl_bluecollar_employee and lcl_whitecollar_employee. The calsses are identical, except for the redifinition of the add_employee method, where the caclculation of wage is different.

Methodes:

Constructor. The constructor is used to initialize the attributes of the employee. Note that the constructor in the supclasss has to be called from within the constructor of the subclass.

Add_Employee. This is a redinition of the same method in the superclass. In the redefined class the wage is calcuated, and the superclass method is called to add the employees to the emploee list.:

The program

REPORT zbc404_hf_events_2 .

*******************************************************

  • Super class LCL_CompanyEmployees

*******************************************************

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

METHODS:

constructor,

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i,

display_employee_list,

display_no_of_employees.

PRIVATE SECTION.

CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

*-- CLASS LCL_CompanyEmployees IMPLEMENTATION

CLASS lcl_company_employees IMPLEMENTATION.

METHOD constructor.

no_of_employees = no_of_employees + 1.

ENDMETHOD.

METHOD add_employee.

  • Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.

l_employee-no = im_no.

l_employee-name = im_name.

l_employee-wage = im_wage.

APPEND l_employee TO i_employee_list.

ENDMETHOD.

METHOD display_employee_list.

  • Displays all employees and there wage

DATA: l_employee TYPE t_employee.

WRITE: / 'List of Employees'.

LOOP AT i_employee_list INTO l_employee.

WRITE: / l_employee-no, l_employee-name, l_employee-wage.

ENDLOOP.

ENDMETHOD.

METHOD display_no_of_employees.

  • Displays total number of employees

SKIP 3.

WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_BlueCollar_Employee

*******************************************************

CLASS lcl_bluecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_hours TYPE i

im_hourly_payment TYPE i,

add_employee REDEFINITION.

PRIVATE SECTION.

DATA:no TYPE i,

name TYPE string,

hours TYPE i,

hourly_payment TYPE i.

ENDCLASS.

*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION

CLASS lcl_bluecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

hours = im_hours.

hourly_payment = im_hourly_payment.

ENDMETHOD.

METHOD add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = hours * hourly_payment.

CALL METHOD super->add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_WhiteCollar_Employee

*******************************************************

CLASS lcl_whitecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_monthly_salary TYPE i

im_monthly_deductions TYPE i,

add_employee REDEFINITION.

PRIVATE SECTION.

DATA:

no TYPE i,

name TYPE string,

monthly_salary TYPE i,

monthly_deductions TYPE i.

ENDCLASS.

*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION

CLASS lcl_whitecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

monthly_salary = im_monthly_salary.

monthly_deductions = im_monthly_deductions.

ENDMETHOD.

METHOD add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = monthly_salary - monthly_deductions.

CALL METHOD super->add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • R E P O R T

*******************************************************

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,

o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

START-OF-SELECTION.

  • Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_hours = 38

im_hourly_payment = 75.

  • Add bluecollar employee to employee list

CALL METHOD o_bluecollar_employee1->add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Create whitecollar employee obeject

CREATE OBJECT o_whitecollar_employee1

EXPORTING im_no = 2

im_name = 'John Dickens'

im_monthly_salary = 10000

im_monthly_deductions = 2500.

  • Add bluecollar employee to employee list

CALL METHOD o_whitecollar_employee1->add_employee

EXPORTING im_no = 1

im_name = 'Karen Johnson'

im_wage = 0.

  • Display employee list and number of employees. Note that the result

  • will be the same when called from o_whitecollar_employee1 or

  • o_bluecolarcollar_employee1, because the methods are defined

  • as static (CLASS-METHODS)

CALL METHOD o_whitecollar_employee1->display_employee_list.

CALL METHOD o_whitecollar_employee1->display_no_of_employees.

The resulting report

List of Employees

1 Karen Johnson 2.850

2 John Dickens 7.500

Total number of employees: 2

3. Interfaces

This example is similiar to th eprevious example, however an interface is implemented with the method add_employee. Note that the interface is only implemented in the superclass ( The INTERFACE stament), but also used in the subclasses.

The interface in the example only contains a method, but an iterface can also contain attrbutes, constants, types and alias names.

The output from example 3 is similiar to the output in example 2.

All changes in the program compared to example 2 are marked with red.

REPORT zbc404_hf_events_3 .

*----


*

  • INTERFACE lif_employee

*----


*

INTERFACE lif_employee.

METHODS:

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i.

ENDINTERFACE.

*******************************************************

  • Super class LCL_CompanyEmployees

*******************************************************

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

INTERFACES lif_employee.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

METHODS:

constructor,

  • add_employee "Removed

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i,

display_employee_list,

display_no_of_employees.

PRIVATE SECTION.

CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

*-- CLASS LCL_CompanyEmployees IMPLEMENTATION

CLASS lcl_company_employees IMPLEMENTATION.

METHOD constructor.

no_of_employees = no_of_employees + 1.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.

l_employee-no = im_no.

l_employee-name = im_name.

l_employee-wage = im_wage.

APPEND l_employee TO i_employee_list.

ENDMETHOD.

METHOD display_employee_list.

  • Displays all employees and there wage

DATA: l_employee TYPE t_employee.

WRITE: / 'List of Employees'.

LOOP AT i_employee_list INTO l_employee.

WRITE: / l_employee-no, l_employee-name, l_employee-wage.

ENDLOOP.

ENDMETHOD.

METHOD display_no_of_employees.

  • Displays total number of employees

SKIP 3.

WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_BlueCollar_Employee

*******************************************************

CLASS lcl_bluecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_hours TYPE i

im_hourly_payment TYPE i,

lif_employee~add_employee REDEFINITION..

PRIVATE SECTION.

DATA:no TYPE i,

name TYPE string,

hours TYPE i,

hourly_payment TYPE i.

ENDCLASS.

*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION

CLASS lcl_bluecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

hours = im_hours.

hourly_payment = im_hourly_payment.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = hours * hourly_payment.

CALL METHOD super->lif_employee~add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_WhiteCollar_Employee

*******************************************************

CLASS lcl_whitecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_monthly_salary TYPE i

im_monthly_deductions TYPE i,

lif_employee~add_employee REDEFINITION.

PRIVATE SECTION.

DATA:

no TYPE i,

name TYPE string,

monthly_salary TYPE i,

monthly_deductions TYPE i.

ENDCLASS.

*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION

CLASS lcl_whitecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

monthly_salary = im_monthly_salary.

monthly_deductions = im_monthly_deductions.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = monthly_salary - monthly_deductions.

CALL METHOD super->lif_employee~add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • R E P O R T

*******************************************************

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,

o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

START-OF-SELECTION.

  • Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_hours = 38

im_hourly_payment = 75.

  • Add bluecollar employee to employee list

CALL METHOD o_bluecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Karen Johnson'

im_wage = 0.

  • Create whitecollar employee obeject

CREATE OBJECT o_whitecollar_employee1

EXPORTING im_no = 2

im_name = 'John Dickens'

im_monthly_salary = 10000

im_monthly_deductions = 2500.

  • Add bluecollar employee to employee list

CALL METHOD o_whitecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Display employee list and number of employees. Note that the result

  • will be the same when called from o_whitecollar_employee1 or

  • o_bluecolarcollar_employee1, because the methods are defined

  • as static (CLASS-METHODS)

CALL METHOD o_whitecollar_employee1->display_employee_list.

CALL METHOD o_whitecollar_employee1->display_no_of_employees.

4. Events

This is the same example as example 4. All changes are marked with red. There have been no canges to the subclasses, only to the superclass and the report, sp the code for th esubclasses is not shown.

For a simple example refer to Events in Examples.

REPORT zbc404_hf_events_4 .

*----


*

  • INTERFACE lif_employee

*----


*

INTERFACE lif_employee.

METHODS:

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i.

ENDINTERFACE.

*******************************************************

  • Super class LCL_CompanyEmployees

*******************************************************

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

  • Declare event. Note that declaration could also be placed in the

  • interface

EVENTS: employee_added_to_list

EXPORTING value(ex_employee_name) TYPE string.

  • CLASS-EVENTS: Events can also be defined as class-events

INTERFACES lif_employee.

METHODS:

constructor,

display_employee_list,

display_no_of_employees,

  • Declare event method

on_employee_added_to_list FOR EVENT employee_added_to_list OF lcl_company_employees

IMPORTING ex_employee_name sender.

PRIVATE SECTION.

CLASS-DATA:

i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

*-- CLASS LCL_CompanyEmployees IMPLEMENTATION

CLASS lcl_company_employees IMPLEMENTATION.

METHOD constructor.

no_of_employees = no_of_employees + 1.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.

l_employee-no = im_no.

l_employee-name = im_name.

l_employee-wage = im_wage.

APPEND l_employee TO i_employee_list.

  • Raise event employee_added_to_list

RAISE EVENT employee_added_to_list

EXPORTING ex_employee_name = l_employee-name.

ENDMETHOD.

METHOD display_employee_list.

  • Displays all employees and there wage

DATA: l_employee TYPE t_employee.

WRITE: / 'List of Employees'.

LOOP AT i_employee_list INTO l_employee.

WRITE: / l_employee-no, l_employee-name, l_employee-wage.

ENDLOOP.

ENDMETHOD.

METHOD display_no_of_employees.

  • Displays total number of employees

SKIP 3.

WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.

METHOD on_employee_added_to_list.

  • Event method

WRITE: / 'Employee added to list', ex_employee_name.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_BlueCollar_Employee

*******************************************************

CLASS lcl_bluecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

See code in example 3...

ENDCLASS.

CLASS lcl_bluecollar_employee IMPLEMENTATION.

See code in example 3...

ENDCLASS.

*******************************************************

  • Sub class LCL_WhiteCollar_Employee

*******************************************************

CLASS lcl_whitecollar_employee DEFINITION

See code in example 3...

ENDCLASS.

CLASS lcl_whitecollar_employee IMPLEMENTATION.

See code in example 3...

ENDCLASS.

*******************************************************

  • R E P O R T

*******************************************************

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,

o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

START-OF-SELECTION.

  • Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1

EXPORTING im_no = 1

im_name = 'Karen Johnson'

im_hours = 38

im_hourly_payment = 75.

  • Register event for o_bluecollar_employee1

SET HANDLER o_bluecollar_employee1->on_employee_added_to_list

FOR o_bluecollar_employee1.

  • Add bluecollar employee to employee list

CALL METHOD o_bluecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Create whitecollar employee obeject

CREATE OBJECT o_whitecollar_employee1

EXPORTING im_no = 2

im_name = 'John Dickens'

im_monthly_salary = 10000

im_monthly_deductions = 2500.

  • Register event for o_whitecollar_employee1

SET HANDLER o_whitecollar_employee1->on_employee_added_to_list

FOR o_whitecollar_employee1.´

  • Add bluecollar employee to employee list

CALL METHOD o_whitecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Display employee list and number of employees. Note that the result

  • will be the same when called from o_whitecollar_employee1 or

  • o_bluecolarcollar_employee1, because the methods are defined

  • as static (CLASS-METHODS)

CALL METHOD o_whitecollar_employee1->display_employee_list.

CALL METHOD o_whitecollar_employee1->display_no_of_employees.

Result:

Employee added to list Karen Johnson

Employee added to list John Dickens

List of Employees

1 Karen Johnson 2.850

2 John Dickens 7.500

Total number of employees: 2

heck the below links lot of info and examples r there

http://www.sapgenie.com/abap/OO/index.htm

http://www.geocities.com/victorav15/sapr3/abap_ood.html

http://www.brabandt.de/html/abap_oo.html

Check this cool weblog:

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://www.allsaplinks.com/

http://www.sap-img.com/

http://www.sapgenie.com/

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://www.sapgenie.com/abap/controls/index.htm

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

these links

http://help.sap.com/saphelp_47x200/helpdata/en/ce/b518b6513611d194a50000e8353423/content.htm

For funtion module to class

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm

for classes

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

for methods

http://help.sap.com/saphelp_47x200/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm

for inheritance

http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm

for interfaces

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm

Former Member
0 Kudos

thanks