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

Interfaces in OO ABAP

Former Member
0 Likes
1,789

Hello Experts,

I am using interfaces in OO ABAP and having problem as shown below .

The CODE is :

interface lif_emp.

   methods:

           add_emp

                importing im_no type i

                          im_name type string

                          im_wage type i .

endinterface.                    "LIF_EMP

*----------------------------------------------------------------------*

*       CLASS lcl_comp_emp DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_comp_emp definition .

   public section .

     interfaces lif_emp .

     types: begin of t_emp,

             no type i,

             name type string,

             wage type i,

           end of t_emp .

     methods:

           constructor ,

                                                       >>>>>>>>>>>(I am Having problem here. Did I miss something)<<<<<<<<<<<<<<<<<<<<

           importing im_no type i

                     im_name type string

                     im_wage type i ,

                     display_emp_list,

                     display_no_of_emp .

   protected section.

   private section .

     class-data : i_emp_list type table of t_emp,

                  no_of_emp type i .

endclass.                    "LCL_COMP_EMP DEFINITION

*----------------------------------------------------------------------*

*       CLASS LCL_COMP_EMP IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_comp_emp implementation .

   method constructor .

     no_of_emp = no_of_emp + 1 .

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp .

     data: l_emp type t_emp .

     l_emp-no = im_no .

     l_emp-name = im_name .

     l_emp-wage = im_wage .

     append l_emp to i_emp_list .

   endmethod .                    "ADD_EMP

   method display_emp_list .

     data: l_emp type t_emp .

     write: / 'LIST OF EMPLOYEES'.

     loop at i_emp_list into l_emp.

       write: / l_emp-no, l_emp-name, l_emp-wage .

     endloop.

   endmethod .                    "DISPLAY_EMP_LIST

   method display_no_of_emp .

     skip 3 .

     write: / 'TOTAL NO OF EMPLOYEES : ', no_of_emp .

   endmethod .                    "DISPLAY_NO_OF_EMP

endclass.                    "LCL_COMP_EMP IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS LCL_BLUECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_bluecollar_employee definition

   inheriting from lcl_comp_emp.

   public section .

     methods :

               constructor

               importing im_no type i

                         im_name type string

                         im_hours type i

                         im_hourly_payment type i,

               lif_emp~add_emp redefinition.

   private section .

     data : no type i,

           name type string,

           hours type i,

           hourly_payment type i.

endclass.                    "LCL_BLUECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*       CLASS LCL_BLUECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_bluecollar_employee implementation .

   method constructor.

     call method super->constructor .

     no = im_no .

     name = im_name .

     hours = im_hours.

     hourly_payment = im_hourly_payment .

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp .

     data : l_wage type i .

     l_wage = hours * hourly_payment.

     call method super->lif_emp~add_emp

       exporting

         im_no   = no

         im_name = name

         im_wage = l_wage.

   endmethod .                    "ADD_EMP

endclass.                    "LCL_BLUECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS LCL_WHITECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_whitecollar_employee definition

        inheriting from lcl_comp_emp.

   public section.

     methods :

             constructor

             importing im_no                 type i

                       im_name               type string

                       im_monthly_salary     type i

                       im_monthly_deducations type i ,

             lif_emp~add_emp redefinition.

   private section .

     data no                    type i,

             name                  type string,

             monthly_salary        type i,

             monthly_deducations    type i.

endclass .                    "LCL_WHITECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*       CLASS LCL_WHITECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_whitecollar_employee implementation .

   method constructor .

     call method super->constructor.

     no = im_no.

     name = im_name.

     monthly_salary = im_monthly_salary.

     monthly_deducations = im_monthly_deducations.

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp.

     data: l_wage type i.

     l_wage = monthly_salary - monthly_deducations.

     call method super->lif_emp~add_emp

       exporting

         im_no   = no

         im_name = name

         im_wage = l_wage.

   endmethod.                    "ADD_EMP

endclass.                    "LCL_WHITECOLLAR_EMPLOYEE IMPLEMENTATION

datao_bluecollar_emp1  type ref to lcl_bluecollar_employee,

        o_whitecollar_emp1 type ref to lcl_whitecollar_employee.

start-of-selection .

   create object o_bluecollar_emp1

     exporting

       im_no             = 1

       im_name           = 'TARAN'

       im_hours          = 38

       im_hourly_payment = 75.

   call method o_bluecollar_emp1->lif_emp~add_emp

     exporting

       im_no   = 1

       im_name = 'TARAN'

       im_wage = 0.

   create object o_whitecollar_emp1

     exporting

       im_no                  = 2

       im_name                = 'MANMEET'

       im_monthly_salary      = 10000

       im_monthly_deducations = 2500.

   call method o_whitecollar_emp1->lif_emp~add_emp

     exporting

       im_no   = 2

       im_name = 'MANMEET'

       im_wage = 0.

   call method o_whitecollar_emp1->display_emp_list.

   call method o_whitecollar_emp1->display_no_of_emp.




Thanks & Regards

Tarandeep Singh

Hello Experts,

I am using interfaces in OO ABAP and having problem as shown below .

The CODE is :

interface lif_emp.

   methods:

           add_emp

                importing im_no type i

                          im_name type string

                          im_wage type i .

endinterface.                    "LIF_EMP

*----------------------------------------------------------------------*

*       CLASS lcl_comp_emp DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_comp_emp definition .

   public section .

     interfaces lif_emp .

     types: begin of t_emp,

             no type i,

             name type string,

             wage type i,

           end of t_emp .

     methods:

           constructor ,

                                                       >>>>>>>>>>>(I am Having problem here. Did I miss something)<<<<<<<<<<<<<<<<<<<<

           importing im_no type i

                     im_name type string

                     im_wage type i ,

                     display_emp_list,

                     display_no_of_emp .

   protected section.

   private section .

     class-data : i_emp_list type table of t_emp,

                  no_of_emp type i .

endclass.                    "LCL_COMP_EMP DEFINITION

*----------------------------------------------------------------------*

*       CLASS LCL_COMP_EMP IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_comp_emp implementation .

   method constructor .

     no_of_emp = no_of_emp + 1 .

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp .

     data: l_emp type t_emp .

     l_emp-no = im_no .

     l_emp-name = im_name .

     l_emp-wage = im_wage .

     append l_emp to i_emp_list .

   endmethod .                    "ADD_EMP

   method display_emp_list .

     data: l_emp type t_emp .

     write: / 'LIST OF EMPLOYEES'.

     loop at i_emp_list into l_emp.

       write: / l_emp-no, l_emp-name, l_emp-wage .

     endloop.

   endmethod .                    "DISPLAY_EMP_LIST

   method display_no_of_emp .

     skip 3 .

     write: / 'TOTAL NO OF EMPLOYEES : ', no_of_emp .

   endmethod .                    "DISPLAY_NO_OF_EMP

endclass.                    "LCL_COMP_EMP IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS LCL_BLUECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_bluecollar_employee definition

   inheriting from lcl_comp_emp.

   public section .

     methods :

               constructor

               importing im_no type i

                         im_name type string

                         im_hours type i

                         im_hourly_payment type i,

               lif_emp~add_emp redefinition.

   private section .

     data : no type i,

           name type string,

           hours type i,

           hourly_payment type i.

endclass.                    "LCL_BLUECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*       CLASS LCL_BLUECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_bluecollar_employee implementation .

   method constructor.

     call method super->constructor .

     no = im_no .

     name = im_name .

     hours = im_hours.

     hourly_payment = im_hourly_payment .

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp .

     data : l_wage type i .

     l_wage = hours * hourly_payment.

     call method super->lif_emp~add_emp

       exporting

         im_no   = no

         im_name = name

         im_wage = l_wage.

   endmethod .                    "ADD_EMP

endclass.                    "LCL_BLUECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS LCL_WHITECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_whitecollar_employee definition

        inheriting from lcl_comp_emp.

   public section.

     methods :

             constructor

             importing im_no                 type i

                       im_name               type string

                       im_monthly_salary     type i

                       im_monthly_deducations type i ,

             lif_emp~add_emp redefinition.

   private section .

     data no                    type i,

             name                  type string,

             monthly_salary        type i,

             monthly_deducations    type i.

endclass .                    "LCL_WHITECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*       CLASS LCL_WHITECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_whitecollar_employee implementation .

   method constructor .

     call method super->constructor.

     no = im_no.

     name = im_name.

     monthly_salary = im_monthly_salary.

     monthly_deducations = im_monthly_deducations.

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp.

     data: l_wage type i.

     l_wage = monthly_salary - monthly_deducations.

     call method super->lif_emp~add_emp

       exporting

         im_no   = no

         im_name = name

         im_wage = l_wage.

   endmethod.                    "ADD_EMP

endclass.                    "LCL_WHITECOLLAR_EMPLOYEE IMPLEMENTATION

datao_bluecollar_emp1  type ref to lcl_bluecollar_employee,

        o_whitecollar_emp1 type ref to lcl_whitecollar_employee.

start-of-selection .

   create object o_bluecollar_emp1

     exporting

       im_no             = 1

       im_name           = 'TARAN'

       im_hours          = 38

       im_hourly_payment = 75.

   call method o_bluecollar_emp1->lif_emp~add_emp

     exporting

       im_no   = 1

       im_name = 'TARAN'

       im_wage = 0.

   create object o_whitecollar_emp1

     exporting

       im_no                  = 2

       im_name                = 'MANMEET'

       im_monthly_salary      = 10000

       im_monthly_deducations = 2500.

   call method o_whitecollar_emp1->lif_emp~add_emp

     exporting

       im_no   = 2

       im_name = 'MANMEET'

       im_wage = 0.

   call method o_whitecollar_emp1->display_emp_list.

   call method o_whitecollar_emp1->display_no_of_emp.




Thanks & Regards

Tarandeep Singh

9 REPLIES 9
Read only

Former Member
0 Likes
1,749

Hi,

methods:

            constructor
*                                                       >>>>>>>>>>>(I am Having problem here. Did I miss something)<<<<<<<<<<<<<<<<<<<<

            importing im_no type i

                      im_name type string

                      im_wage type i ,

                      display_emp_list,

                      display_no_of_emp .


Please check syntax check once you get the ans.


Thanks

Satish

Read only

0 Likes
1,749

Thanks for reply.....But its not working

Read only

former_member302911
Active Participant
0 Likes
1,749

The comma ?  Remove it

methods:

           constructor , <<<<<<<<

                                                       >>>>>>>>>>>(I am Having problem here. Did I miss something)<<<<<<<<<<<<<<<<<<<<

           importing im_no type i

                     im_name type string

                     im_wage type i ,

Read only

Former Member
0 Likes
1,749

No ,

If i erase comma then i'll get the error

class lcl_bluecollar_employee implementation .

   method constructor.

     call method super->constructor . << obligatory parameter "IM_NO" had no value assigned to it>>

     no = im_no .

     name = im_name .

     hours = im_hours.

     hourly_payment = im_hourly_payment .

   endmethod .                    "CONSTRUCTOR

Read only

0 Likes
1,749

Tarandeep,

i see some problem here...

No need to set the dot at end of any instruction.

You miss the EXPORTING statement.

The parameters are inverted.

The correct syntax is this:

call method super->constructor

     EXPORTING

     im_no       = no

     im_name = name

     im_hours = hours

     im_hourly_payment  = hourly_payment .

or this, in short format

call method super->constructor( im_no       = no

                                                im_name = name

                                                im_hours = hours

                                                im_hourly_payment  = hourly_payment ).

Regards,

Angelo.

Read only

0 Likes
1,749

No sir,

Now it is asking for a period (.)  after Constructor .

Read only

0 Likes
1,749

Hi,

Formal parameter declaration using IMPORTING, in Calling area using EXPORTING (Use vice versa )

In your case use only below way in calling time

      call method super->constructor
      EXPORTING
         im_no = no
         im_name = name
         im_wage = hours.


Thanks

Satish

Read only

0 Likes
1,749

No Sir,

Its not working because we also have hourly_payment in SUBCLASS LCL_BLUECOLLAR_EMPLOYEE and

   monthly_salary = im_monthly_salary.

     monthly_deducations = im_monthly_deducations.

into 2nd SUBCLASS LCL_WHITECOLLAR_EMPLOYEE .


Where is the error exactly......PLEASE find it .

Thanks

Taran

Read only

Former Member
0 Likes
1,749

GOT THE CORRECT ANSWER

HERE IS THE RIGHT CODE :

report  zooabap_interface.

*----------------------------------------------------------------------*

*       INTERFACE LIF_EMP

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

interface lif_emp.

   methods:

           add_emp

                importing im_no type i

                          im_name type string

                          im_wage type i .

endinterface.                    "LIF_EMP

*----------------------------------------------------------------------*

*       CLASS lcl_comp_emp DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_comp_emp definition .

   public section .

     interfaces lif_emp .

     types: begin of t_emp,

             no type i,

             name type string,

             wage type i,

           end of t_emp .

     methods:

           constructor,

          display_emp_list ,

          display_no_of_emp .

   private section .

     class-data : i_emp_list type table of t_emp,

                  no_of_emp type i .

endclass.                    "LCL_COMP_EMP DEFINITION

*----------------------------------------------------------------------*

*       CLASS LCL_COMP_EMP IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_comp_emp implementation .

   method constructor .

     no_of_emp = no_of_emp + 1 .

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp .

     data: l_emp type t_emp .

     l_emp-no = im_no .

     l_emp-name = im_name .

     l_emp-wage = im_wage .

     append l_emp to i_emp_list .

   endmethod .                    "ADD_EMP

   method display_emp_list .

     data: l_emp type t_emp .

     write: / 'LIST OF EMPLOYEES'.

     loop at i_emp_list into l_emp.

       write: / l_emp-no, l_emp-name, l_emp-wage .

     endloop.

   endmethod .                    "DISPLAY_EMP_LIST

   method display_no_of_emp .

     skip 3 .

     write: / 'TOTAL NO OF EMPLOYEES : ', no_of_emp .

   endmethod .                    "DISPLAY_NO_OF_EMP

endclass.                    "LCL_COMP_EMP IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS LCL_BLUECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_bluecollar_employee definition

   inheriting from lcl_comp_emp.

   public section .

     methods :

               constructor

               importing im_no type i

                         im_name type string

                         im_hours type i

                         im_hourly_payment type i,

               lif_emp~add_emp redefinition.

   private section .

     data : no type i,

           name type string,

           hours type i,

           hourly_payment type i.

endclass.                    "LCL_BLUECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*       CLASS LCL_BLUECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_bluecollar_employee implementation .

   method constructor .

     call method super->constructor .

      no = im_no .

      name = im_name .

      hours = im_hours .

      hourly_payment = im_hourly_payment    .

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp .

     data : l_wage type i .

     l_wage = hours * hourly_payment.

     call method super->lif_emp~add_emp

       exporting

         im_no   = no

         im_name = name

         im_wage = l_wage.

   endmethod .                    "ADD_EMP

endclass.                    "LCL_BLUECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS LCL_WHITECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_whitecollar_employee definition

        inheriting from lcl_comp_emp.

   public section.

     methods :

             constructor

             importing im_no                 type i

                       im_name               type string

                       im_monthly_salary     type i

                       im_monthly_deducations type i ,

             lif_emp~add_emp redefinition.

   private section .

     data no                    type i,

             name                  type string,

             monthly_salary        type i,

             monthly_deducations    type i.

endclass .                    "LCL_WHITECOLLAR_EMPLOYEE  DEFINITIO

*----------------------------------------------------------------------*

*       CLASS LCL_WHITECOLLAR_EMPLOYEE IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

class lcl_whitecollar_employee implementation .

   method constructor .

     call method super->constructor.

     no = im_no.

     name = im_name.

     monthly_salary = im_monthly_salary.

     monthly_deducations = im_monthly_deducations.

   endmethod .                    "CONSTRUCTOR

   method lif_emp~add_emp.

     data: l_wage type i.

     l_wage = monthly_salary - monthly_deducations.

     call method super->lif_emp~add_emp

       exporting

         im_no   = no

         im_name = name

         im_wage = l_wage.

   endmethod.                    "ADD_EMP

endclass.                    "LCL_WHITECOLLAR_EMPLOYEE IMPLEMENTATION

datao_bluecollar_emp1  type ref to lcl_bluecollar_employee,

        o_whitecollar_emp1 type ref to lcl_whitecollar_employee.

start-of-selection .

   create object o_bluecollar_emp1

     exporting

       im_no             = 1

       im_name           = 'TARAN'

       im_hours          = 38

       im_hourly_payment = 75.

   call method o_bluecollar_emp1->lif_emp~add_emp

     exporting

       im_no   = 1

       im_name = 'TARAN'

       im_wage = 0.

   create object o_whitecollar_emp1

     exporting

       im_no                  = 2

       im_name                = 'MANMEET'

       im_monthly_salary      = 10000

       im_monthly_deducations = 2500.

   call method o_whitecollar_emp1->lif_emp~add_emp

     exporting

       im_no   = 2

       im_name = 'MANMEET'

       im_wage = 0.

   call method o_whitecollar_emp1->display_emp_list.

   call method o_whitecollar_emp1->display_no_of_emp.







Thanks for reply guys


Tarandeep Singh