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 accessible from outside
* ------------------------------------------------- -------------------
Types:
Begin of t_employee,
NO TYPE I,
Name Type String,
END OF T_EMPLOYEE.
Methods:
Constructor
IMPORTING IM_EMPLOYE_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 Area Global for All Instances
Class-data: g_no_of_employees type I.
PRIVATE section.
* ------------------------------------------------- -------------------
* The private section is only access from forin the classs
* ------------------------------------------------- -------------------
Data: g_employee type t_employee.
Endclass.
* --- LCL Employee - ImplementClass 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 redined 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 employeesNote 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! is calcuated, and the superclass method is caled to add the Employees to the Emploe 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 ire 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 employeesdata: 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_EMPLOYEEEEEE
*********************************************************** *****
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_EMPLOYEEEE
*********************************************************** *****
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_WHITECOLLLAR_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_EMPLOYE.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 area defined
* AS Static (Class-Methods)
Call method o_whitecollar_employee1-> display_employee_list.
Call method o_whitecollar_employee1-> display_no_of_employees.
Resulting report
List of Employees1 Karen Johnson 2.8502 John Dickens 7.500Total 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 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 ire 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_EMPLOYEEEEEE
*********************************************************** *****
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_EMPLOYEEEE
*********************************************************** *****
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_WHITECOLLLAR_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_employeeexporting 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 area 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 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 EventMETHOD
ON_EMPLOYEE_ADDED_TO_LIST for EVENT EMPLOYE_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_EMPLOYE_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_EMPLOYE_NAME.ENDMETHOD.
Endclass.
*********************************************************** *****
* SUB CLASS LCL_BLUECOLLAR_EMPLOYEEEEEE
*********************************************************** *****
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_EMPLOYEEEE
*********************************************************** *****
Class LCL_WHITECOLLAR_EMPLOYEE DEFINITION
See Code in Example 3 ...
Endclass.
Class LCL_WHITECOLLLAR_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_BLUECOLLLAR_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 = 10000IM_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 area 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 JohnsoneMployee Added to List John Dickenslist of Employees1 Karen Johnson 2.8502 John Dickens 7.500 Total Number of Employees: 2