You are on page 1of 10
INHERITANCE & POLYMORPHISM Prnsinls Create super class LCL_ComponyEmployces The clas hs the motheds Constructor + Add_Employce- Add « new employee t thelist oF employees + Display Employec_List Displays all employees und there wage ¢ ‘Dilplay no af ouplayees- Diaplays tal aber Sh eniployees Note the use of CLASS-DATA to beep the list of employees and aumber af employece the same from instance to insinace. Create subclasses fel_binecollar-erployee and Iol_vhitecollar_cmpleyee: The caluses see ‘lentical, except for tac redifinition ofthe add_employce method, where the eaclewlaion of wage is different Methods 4+ Constructor. The constracor is used to inital the atirbutes of the employee [Note that the constrvcior inthe subelace hae fo be calle from within the constrictor of the subclass. + Ada Employee. This ina eedefintion ofthe came method inthe wepeislase Ta the redcfincd clise whe wage is caleuated, and tne superclass method is called Wo ade the employees tothe emploce list, The pn REPORT vhc404_Hf_cvents_? TYPES: BEGIN OF « cmployce, no TYPE 1, mame TYPE sting, swage TYPE 1 END OF t employee. METHODS: ‘sdd_cmployee IMPORTING imino TYPE i fm_name TYPE string INHERITANCE & POLYMORPHISM 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 lel_bluecollar_employee and Icl_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 subclass has to be called from within the constructor of the subclass. + Add_Employee. This is a redefinition 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 emploce list. The program REPORT zbc404_hf_events_2 Steere snnirenmerenninennsasenenenenetenenster # Super class Lc CLASS Icl_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 CompanyEmployces #822 e REMERON CEIeReRRNARCESENESRNEE IMPORTING im_no TYPE i im_name TYPE string jim_wage TYPE i Alapiay-ompleyoe_ Fe, display no_of employees. PRIVATE SECTION. CLASS-DATA: | employee list TYPE TABLE OF Lemphsce. ‘ne_of-employece TYPE i ENDCLASS. = CLASS LOL_CompuayEmployecs IMPLEMENTATION ‘CLASS Ie company_employees IMPLEMENTATION. METHOD constructor. ro_s_employcer = s0_of employees + 1 ENDMETHOD, METHOD add_employce ‘dl cw omipoyer wo the Lat employees DATA: Lemployce TYPE (employee {ie playees |Cemployce-rame = im_name [Cemployce-wage = imavage APPEND L_employee TO {_employee_tist ENDMETHOD, METHOD display_cmployce_ tit Displaysall employees and there wage DATA: employee TTE Lemplayee WRITE: Listof Employees. LOOP AT employee list INTO employee. WRITE: /Lemployee-no. Lemployee-name. I_employec-wage. ENDLOo?. ENDMETHOD. METHOD diaplay_no_of employees Displays total numberof employers SkIP3 WRITE: / Total umber of employecs:'.no_of employees. ENDMETHOD. ENDCLASS. * Sob clate LCL BlucColle. Employee CLASS IeL bluccollar_employee DEFINITION INHERITING FROM Iel_company_employees. PUBLIC SECTION. METHODS. 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 Iel_company_employees IMPLEMENTATION. METHOD constructor. no_of_employees = no_of_employces + 1, ENDMETHOD. METHOD add_employee. * Adds a new employee to the list of employees DATA: _employce TYPE t_employee. |_employce-no = im_no. |_employce-name = im_name, |_employee-wage = im_wage. APPEND |_employee TO i_employee_list. ENDMETHOD. METHOD display_employee_list. * Displays all employees and there wage DATA: |_employee TYPE t_employee. WRITE: / ‘List of Employees’. LOOP AT i_employee_list INTO |_employee. WRITE: / |_employee-no, |_employee-name, |_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. SRO SEES HEED ODEO HOODEO REE ODESSA Sub class LCL_BlueCollar_Employee shetetenetenenenetsnsrerstensesesesceesesssesessnsesee® CLASS cl_bluecollar_employee DEFINITION INHERITING FROM lel_company_employees. PUBLIC SECTION. METHODS: constructor IMPORTING immo TYPE’ imme TYPE string imchours TYPE: im_hourly_paymest TYPE i add_employee REDEFINITION PRIVATE SECTION. DATA:no TYPE: same TYPE string ous TYPE: ourly_ payment TYPE i ENDCLASS. += CLASS LCL RincCollar_Fmployes IMPLEMENTATION (CLASS IeL_binecollar_employee IMFLEMINTATION. METHOD constructor. “The superclass constructor meting must be call from the sabelass constructor method CALL METHOD super constrictor hous. hously_payment =im_howrly_poyment ENDMETHOD. METHOD add_employce © Calculate wage un all the seperelis melo add_ employee tad the employee the caaployee lint DATA: 1 wage TYPE i |Lwage = hour * howrly_ payment. CALL METHOD super >adé_employce EXFORTING im_no = ne ENDMBTHOD. ENDCLASS, + Sub clase LCL_WhiteCollar_Employce CLASS IeL_whitecollar_employee DEFINITION INHERITING FROM lel_company_employees. PUBLIC SECTION, METHODS. 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. Iel_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_houtly_payment. ENDMETHOD. METHOD add_employee. * Calculate ‘wage an call the superclass method add_employee to add * the employee to the employee list DATA: |_wage TYPE i. |_wage = hours * hourly_payment. CALL METHOD super- >add_employee EXPORTING im_no = no im_name = name im_wage ~ |_wage, ENDMETHOD. ENDCLASS. JEG econo ee oeeiadenebernetenees # Sub class LCL_WhiteCollar Employee ROOD OUCH CLASS Icl_whitecollar_employee DEFINITION INHERITING FROM Iel_company_employees. PUBLIC SECTION. METHODS: constructor IMPORTING im_n0 TYPE’ ipomoe TYPE string im_monthly_silory TYPE: im_soonthly-deduetiony TYPE i add_employee REDEFINITION. PRIVATE SECTION. DATA: oa TYPE, same TYPE string, sonthlysalury TYPE, monthly deductions TYPE ENDCLASS. CLASS LCL_WhiteCollar Employee IMPLEMENTATION CLASS IeL_whitecollar_employee IMPLEMENTATION. METHOD constrecar. ‘The sinpestqan cs oat wind fae Ue ict iden OE Sata leper wetted CALL METHOD super scoastractor Bola) my aacany ey el sede sois = Se ny ee ERDME HOD: METHOD add_employce © Calculate wage un all the seperelis melo add_ employee tad the iiple joe 1 te cap te DATA: 1 wage TYPE i Leaps jonthlysslary ~ menthly_ deductions CALL METHOD super>add_employce EXFORTING im_no = ne ENDMBTHOD. ENDCLASS. “REPORT pata soba 0 plsccetlar_cmployce! TYFE REF TO lel_bluccollas_cmployce, ewhitecollar employee! TYPE REF TO Isi_whitccollar_emplayec START-OF-SELECTION. 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 Icl_whitecollar_employee IMPLEMENTATION M HOD 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: _wage TYPE i. |_wage = monthly_salary - monthly_deductions, CALL METHOD super->add_employee EXPORTING im_no = no im_name = name im_wage = |_wage. ENDMETHOD. ENDCLASS. LSSea Hoon ra a soooce a aoooca ssoooonaenccersasr © RE PORT He ecco nooo a nema EsonanaEsie DATA: * Object references 0_bluecollar_employeel TYPE REF TO lel_bluecollar_employee, o_whitecollar_employee1 TYPE REF TO lel_whitecollar_employee, START-OF-SELECTION. * Create bluccollar employee object CREATE OBJECT o_bluccollas-cmploycel EXFORTING im_no isms = pile Kasei tm hours = 38 in Joy pies 675; * Add bluccollar employee to cmployce list CALL METHOD o_bluccollar_employcel->add_employce EXPORTING im_no is_yieme = Gylle Keita Cite whitccollar employee abejeet CREATE OBJECT o_whitevellar employee! EXPORTING im_on = ‘ona Dicksns im _montly_salary = 10000 {im _mnnthly deductions = 2500, Ad usar plage ta Ciployes Ik CALL METHOD o_whitccollar_cmployce!->dd_employce EXPORTING im=no fm_same = Koren Jebnsoa! + Display cmployce list and number of emplayces. Note tha the regu, wil be renting how called Fone whitecelk-enipoye oe + e_bluccolaellar employee L becouse the methods are defined ss suite (CLASS-METHODS) CALL METHOD o_whitecollar_employcel->display_employee_is, CALE METHOD o_whitccellar_employce |>display ne of employees ourrur The reselling report Listof Employecs 1 Keren Sohne 2.850 2 John Dickens 7300 Told winter at mappa * Create bluecollar employee obeject CREATE OBJECT o0_bluecollar_employee! EXPORTING im_no = 1 im_name ='Gylle Karen’ im_hours = 38 im_hourly_payment = 75. * Add bluecollar employee to employee list CALL METHOD 0_bluccollar_employee!->add_employce EXPORTING im_no =I im_name ='Gylle Karen’ im_wage = 0. * Create whitecollar employee obeject CREATE OBJECT o_whitecollar_employeel EXPORTING im_no=2 im_name = John Dickens’ im_monthly_salary = 10000 im_monthly_deductions = 2500. * Add bluecollar employee to employee list CALL METHOD o0_whitecollar_employee’ EXPORTING im_no= I \dd_employee 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_employee! or * o_bluecolarcollar_employee1, because the methods are defined * as static (CLASS-METHODS) CALL METHOD o_whitecollar_employee1->display_employee_list. CALL METHOD 0_whitecollar_employee1 >display_no_of_employees. OUTPUT The resulting report: List of Employees 1 Karen Johnson 2.850 2 John Dickens 7.500 Total number of employees: 2

You might also like