Application Development and Automation 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: 
Read only

oops

Former Member
0 Likes
538

HI,

PLEASE SEND SOME EXAMPLE FOR INHERITANCE .

THANK YOU

ASHOK KUMAR

4 REPLIES 4
Read only

Former Member
0 Likes
510

Hi

REPORT YSUBDEL.

CLASS C1 DEFINITION.

PUBLIC SECTION.

METHODS : METH1.

DATA : NUM TYPE I VALUE 6.

PROTECTED SECTION.

DATA : num2 type i value 7.

METHODS METH2.

ENDCLASS.

CLASS C1 IMPLEMENTATION .

METHOD : METH1.

WRITE:/5 num.

endmethod.

METHOD : METH2.

WRITE:/5 ' I am meth2 '.

ENDMETHOD.

ENDCLASS.

CLASS C2 DEFINITION INHERITING FROM C1.

PUBLIC SECTION.

METHODS : M1.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

METHOD M1.

CALL METHOD : meth1, meth2.

write:/5 num2.

endmethod.

endclass.

START-OF-SELECTION.

DATA : OREF TYPE REF TO C2.

CREATE OBJECT OREF.

REPORT YSUBDEL.

CLASS C1 DEFINITION.

PUBLIC SECTION.

METHODS : METH1.

PROTECTED SECTION.

METHODS METH2.

ENDCLASS.

CLASS C1 IMPLEMENTATION .

METHOD : METH1.

WRITE:/5 'I am meth1 in class C1'.

CALL METHOD METH2.

ENDMETHOD.

METHOD : METH2.

WRITE:/5 ' I am meth2 in class C1 '.

ENDMETHOD.

ENDCLASS.

CLASS C2 DEFINITION INHERITING FROM C1.

PUBLIC SECTION.

METHODS : METH1 redefinition .

PROTECTED SECTION.

METHODS : METH2 redefinition.

ENDCLASS.

CLASS C2 IMPLEMENTATION.

METHOD METH1.

WRITE:/5 'I am meth1 in class C2'.

call method meth2.

endmethod.

METHOD : METH2.

WRITE:/5 ' I am meth2 in class C2 '.

ENDMETHOD.

endclass.

START-OF-SELECTION.

DATA : OREF1 TYPE REF TO C1 ,

OREF2 TYPE REF TO C2.

CREATE OBJECT : OREF1 , OREF2.

CALL METHOD : OREF1->METH1 ,

OREF2->METH1.

<b>Reward points for useful Answers</b>

Regards

Anji

Read only

sridevi_p
Active Contributor
0 Likes
510

Hi,

Go to tcode - ABAPDOCU

U get the list of ABAP documetns.

In it, cehck ABAP Objects --> Inheritance --> Inheritance in ABAP Objects

Tha adv is that u can run and learn at ease.

Regards,

Sridevi

<i>* Pls. assign poitns, if useful</i>

Read only

Former Member
0 Likes
510

hi

check with the following prog... just implemented simple functionalities

&----


*& Report ZABAPOOP_INHERITANCE

*&

&----


*&

*&

&----


REPORT ZABAPOOP_INHERITANCE.

  • begin of definition of a local class

class lcl_employee definition.

  • public section is visible outside the class, generally methods are created here

public section.

types:

begin of t_employees,

no_emp type i,

name type string,

end of t_employees.

  • declaration of methods

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

  • protected section

protected section.

class-data: g_no_of_emp type i.

private section.

data: g_employees type t_employees.

endclass.

class lcl_employee implementation.

method: constructor.

g_employees-no_emp = im_employee_no .

g_employees-name = im_employee_name.

endmethod.

method display_employee.

write 😕 'Employee', g_employees-no_emp ,g_employees-name .

endmethod.

method : display_no_of_emp.

write:/ 'Number of employees is:' , g_no_of_emp .

endmethod.

endclass.

data: g_emp_1 type ref to lcl_employee,

g_emp_2 type ref to lcl_employee.

start-of-selection.

create object g_emp_1

exporting im_employee_no = 1

im_employee_name = 'Dinesh Dhiman'.

create object g_emp_2

exporting im_employee_no = 2

im_employee_name = 'Ravish Goyal'.

CALL METHOD g_emp_1->display_employee.

CALL METHOD g_emp_2->display_employee.

Hope this helps to solve ur problem....

<b>do reward if useful....</b>

regards

dinesh

Read only

Former Member
0 Likes
510

Hi Ashok

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.

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

You have a lot of threads open .

We would really appreciate that you close your threads and also reward all helpful answers.

Reward if useful

Regards,

Suruchi