cancel
Showing results for 
Search instead for 
Did you mean: 

Example program on inheritance?

Former Member
0 Kudos

Hi Experts,

I need one example program on inheritance.

I saw demo program but i feel it is complicated.

if any one is having please send me one program.

Thanks,

vijay

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

hi,

check out this program. i ll helpful for you.

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_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.

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.

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.

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_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.

CALL METHOD o_bluecollar_employee1->display_employee_list.

for further more detail, refer this site.

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

regards,

prema.A

Answers (5)

Answers (5)

vinod_gunaware2
Active Contributor
0 Kudos

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

regards

vinod

Former Member
0 Kudos

Hi,

Refer this link :

http://help.sap.com/saphelp_nw04/helpdata/en/1d/df5f57127111d3b9390000e8353423/frameset.htm

REPORT  ZGH_INHERITANCE                        .

CLASS counter DEFINITION.
  PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i,
             increment,
             get EXPORTING value(get_value) TYPE i.
  PROTECTED SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
  METHOD get.
  break-point.
    get_value = count.
  ENDMETHOD.
ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.
  PUBLIC SECTION.
    METHODS increment REDEFINITION.
    DATA count_ten(1) TYPE c.
ENDCLASS.

CLASS counter_ten IMPLEMENTATION.
  METHOD increment.
    DATA modulo TYPE i.
    CALL METHOD super->increment.
    WRITE / count.
    modulo = count MOD 10.
    IF modulo = 0.
      count_ten = count_ten + 1.
      WRITE count_ten.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

DATA: count TYPE REF TO counter,
      number TYPE i VALUE 5.

START-OF-SELECTION.

  CREATE OBJECT count TYPE counter_ten.
  break-point.

  CALL METHOD count->set EXPORTING set_value = number.

  DO 20 TIMES.
    CALL METHOD count->increment.

Regards,

Gayathri

Former Member
0 Kudos

hai vijay,

here is the simple program on inheritance.

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

CLASS COMP_EMPLOYEE DEFINITION.

PUBLIC SECTION.

TYPES: BEGIN OF T_EMPLOYEE,

NUM TYPE I,

NAME TYPE STRING,

WAGES TYPE I,

END OF T_EMPLOYEE.

METHODS: CONSTRUCTOR,

ADD_EMPLOYEE

IMPORTING METH_NUM TYPE I

METH_NAME TYPE STRING

METH_WAGES TYPE I,

DISPLAY_EMP_LIST,

DISPLAY_NUM_EMP.

PRIVATE SECTION.

CLASS-DATA: I_EMP_LIST TYPE TABLE OF T_EMPLOYEE,

NUM_EMP TYPE I.

ENDCLASS. "COMP_EMPLOYEE DEFINITION

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

CLASS COMP_EMPLOYEE IMPLEMENTATION.

METHOD CONSTRUCTOR.

NUM_EMP = NUM_EMP + 1.

ENDMETHOD. "CONSTRUCTOR

METHOD ADD_EMPLOYEE.

DATA: L_EMPLOYEE TYPE T_EMPLOYEE.

L_EMPLOYEE-NUM = METH_NUM.

L_EMPLOYEE-NAME = METH_NAME.

L_EMPLOYEE-WAGES = METH_WAGES.

APPEND L_EMPLOYEE TO I_EMP_LIST.

ENDMETHOD. "ADD_EMPLOYEE

METHOD DISPLAY_EMP_LIST.

DATA: L_EMPLOYEE TYPE T_EMPLOYEE.

WRITE: / 'LIST OF EMPLOYEES'.

LOOP AT I_EMP_LIST INTO L_EMPLOYEE.

WRITE: / 'EMP ID', L_EMPLOYEE-NUM, / 'EMP NAME', L_EMPLOYEE-NAME,

/ 'WAGES', L_EMPLOYEE-WAGES.

ENDLOOP.

ENDMETHOD. "DISPLAY_EMP_LIST

METHOD DISPLAY_NUM_EMP.

WRITE: / 'NUMBER OF EMPLOYEES', NUM_EMP.

ENDMETHOD. "DISPLAY_NUM_EMP

ENDCLASS. "COMP_EMPLOYEE

****inheritance********************************

CLASS COMP_EMP_BC DEFINITION INHERITING FROM COMP_EMPLOYEE.

PUBLIC SECTION.

METHODS: CONSTRUCTOR

IMPORTING METH_NUM TYPE I

METH_NAME TYPE STRING

METH_HOURS TYPE I

METH_HOURS_PAY TYPE I,

ADD_EMPLOYEE REDEFINITION.

PRIVATE SECTION.

DATA: NUM TYPE I,

NAME TYPE STRING,

HOURS TYPE I,

HOURS_PAY TYPE I.

ENDCLASS. "COMP_EMP_BC DEFINITION

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

CLASS COMP_EMP_BC IMPLEMENTATION.

METHOD CONSTRUCTOR.

CALL METHOD SUPER->CONSTRUCTOR.

NUM = METH_NUM.

NAME = METH_NAME.

HOURS = METH_HOURS.

HOURS_PAY = METH_HOURS_PAY.

ENDMETHOD. "CONSTRUCTOR

METHOD ADD_EMPLOYEE.

DATA: L_WAGES TYPE I.

L_WAGES = HOURS * HOURS_PAY.

CALL METHOD SUPER->ADD_EMPLOYEE

EXPORTING

METH_NUM = NUM

METH_NAME = NAME

METH_WAGES = L_WAGES.

ENDMETHOD. "ADD_EMPLOYEE

ENDCLASS. "COMP_EMP_BC IMPLEMENTATION

                                                • inheritance***************

CLASS COMP_EMP_WC DEFINITION INHERITING FROM COMP_EMPLOYEE.

PUBLIC SECTION.

METHODS: CONSTRUCTOR

IMPORTING METH_NUM TYPE I

METH_NAME TYPE STRING

METH_MON_SAL TYPE I

METH_MON_DED TYPE I,

ADD_EMPLOYEE REDEFINITION.

PRIVATE SECTION.

DATA: NUM TYPE I,

NAME TYPE STRING,

MON_SAL TYPE I,

MON_DED TYPE I.

ENDCLASS. "COMP_EMP_WC DEFINITION

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

CLASS COMP_EMP_WC IMPLEMENTATION.

METHOD CONSTRUCTOR.

CALL METHOD SUPER->CONSTRUCTOR.

NUM = METH_NUM.

NAME = METH_NAME.

MON_SAL = METH_MON_SAL.

MON_DED = METH_MON_DED.

ENDMETHOD. "CONSTRUCTOR

METHOD ADD_EMPLOYEE.

DATA: L_WAGES TYPE I.

L_WAGES = MON_SAL - MON_DED.

CALL METHOD SUPER->ADD_EMPLOYEE

EXPORTING

METH_NUM = NUM

METH_NAME = NAME

METH_WAGES = L_WAGES.

ENDMETHOD. "ADD_EMPLOYEE

ENDCLASS. "COMP_EMP_WC IMPLEMENTATION

              • report**********************************

DATA: COMP_EMP_BC1 TYPE REF TO COMP_EMP_BC,

COMP_EMP_WC1 TYPE REF TO COMP_EMP_WC.

START-OF-SELECTION.

CREATE OBJECT COMP_EMP_BC1

EXPORTING METH_NUM = 1

METH_NAME = 'XYZ'

METH_HOURS = 48

METH_HOURS_PAY = 70.

CALL METHOD COMP_EMP_BC1->ADD_EMPLOYEE

EXPORTING

METH_NUM = 1

METH_NAME = 'XYZ'

METH_WAGES = 0.

CREATE OBJECT COMP_EMP_WC1

EXPORTING METH_NUM = 2

METH_NAME = 'ABC'

METH_MON_SAL = 25000

METH_MON_DED = 5000.

CALL METHOD COMP_EMP_WC1->ADD_EMPLOYEE

EXPORTING

METH_NUM = 2

METH_NAME = 'ABC'

METH_WAGES = 0.

CALL METHOD COMP_EMP_BC1->DISPLAY_EMP_LIST.

CALL METHOD COMP_EMP_BC1->DISPLAY_NUM_EMP.

hope this is helpful.

regards,

praba.

Former Member
0 Kudos

Hai vijay........

Here is the sample code...

REPORT demo_inheritance.

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS: set IMPORTING value(set_value) TYPE i,

increment,

get EXPORTING value(get_value) TYPE i.

PROTECTED SECTION.

DATA count TYPE i.

ENDCLASS.

CLASS counter IMPLEMENTATION.

METHOD set.

count = set_value.

ENDMETHOD.

METHOD increment.

ADD 1 TO count.

ENDMETHOD.

METHOD get.

get_value = count.

ENDMETHOD.

ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.

PUBLIC SECTION.

METHODS increment REDEFINITION.

DATA count_ten(1) TYPE c.

ENDCLASS.

CLASS counter_ten IMPLEMENTATION.

METHOD increment.

DATA modulo TYPE i.

CALL METHOD super->increment.

WRITE / count.

modulo = count MOD 10.

IF modulo = 0.

count_ten = count_ten + 1.

WRITE count_ten.

ENDIF.

ENDMETHOD.

ENDCLASS.

DATA: count TYPE REF TO counter,

number TYPE i VALUE 5.

START-OF-SELECTION.

CREATE OBJECT count TYPE counter_ten.

CALL METHOD count->set EXPORTING set_value = number.

DO 20 TIMES.

CALL METHOD count->increment.

ENDDO.

Also refer this link..

http://help.sap.com/saphelp_46b/helpdata/en/c8/c7f9bdaa28d3119b440060b0671acc/frameset.htm

Regards,

Srikanth.

Plz reward points if helpful..

former_member188685
Active Contributor
0 Kudos

Hi,

Check this Example <b>demo_inheritance</b>

Regards

Vijay