Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
15Activity
0 of .
Results for:
No results containing your search query
P. 1
Step by Step Example of Abap Objects

Step by Step Example of Abap Objects

Ratings: (0)|Views: 427 |Likes:
Published by api-3834175

More info:

Published by: api-3834175 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

This example is a step by step example, moving from a simple clas to more
sophisticated classes, and covers inheritance, interfaces and events.

1.Simple class
2.Inheritance and ploymorphism
3.Interfaces
4.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.
*********************************************************************
*LCL_EMPLOYEE
*********************************************************************
*---- 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.
************************************************************************
*REPORT
*********************************************************************
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 superclasslcl_company_employees and two subclasses
lcl_bluecollar_employeeand 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 methodadd_employe e, 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:

\u2022
Constructor
\u2022
Add_Employee - Adds a new employee to the list of employees
\u2022
Display_Employee_List - Displays all employees and there wage
\u2022
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 subclasseslcl_bluecollar_e mployee andlcl_whitecol lar_e mployee. The
calsses are identical, except for the redifinition of the add_employee method,
where the caclculation of wage is different.

Methodes:
\u2022

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.

\u2022

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.

Activity (15)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
jjitendra84 liked this
jjitendra84 liked this
vrajaman liked this
amit liked this
vsagarc liked this
dineshkaushal liked this
Danai Piya liked this
lukkshme liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->