You are on page 1of 5

Example: Polymorphism Scenario

REPORT Z730OOPS25.

class lcl_enquiry DEFINITION.
  PUBLIC SECTION.
    methods : calc_disc IMPORTING i_fees type i,
              display_fees.
  PROTECTED SECTION.
    data totalfees type i.
endclass.

class lcl_enquiry IMPLEMENTATION.

  method calc_disc.
       totalfees = ( i_fees - i_fees * 5 / 100 ).
  endmethod.

  method display_fees.
    write :/ 'Total Fees after discount :',totalfees.
  ENDMETHOD.

endclass.

class lcl_enquiry_r DEFINITION INHERITING FROM lcl_enquiry.
  PUBLIC SECTION.
      methods calc_disc REDEFINITION.
endclass.

class lcl_enquiry_r IMPLEMENTATION.

  method calc_disc.
    totalfees = ( i_fees - i_fees * 10 / 100 ).
  ENDMETHOD.

endclass.
class lcl_enquiry_v DEFINITION INHERITING FROM lcl_enquiry.
  PUBLIC SECTION.
      methods calc_disc REDEFINITION.
endclass.

class lcl_enquiry_v IMPLEMENTATION.

  method calc_disc.
    totalfees = ( i_fees - i_fees * 15 / 100 ).
  ENDMETHOD.

endclass.

data ob type ref to lcl_enquiry.

START-OF-SELECTION.
PARAMETERS : p_fees type i,
             p_no_enq type i.

if p_no_enq > 0.
  case p_no_enq.
    when 1.
      create object ob type lcl_enquiry.
      call method ob->calc_disc
      exporting
        i_fees = p_fees.
    when 2 or 3.
      create object ob type lcl_enquiry_r.
      call method ob->calc_disc
      exporting
        i_fees = p_fees.
    when others.
      create object ob type lcl_enquiry_v.
      call method ob->calc_disc
      exporting
        i_fees = p_fees.
  endcase.
  call method ob->display_fees.
else.
  message 'Please enter Positive values for no.of enquiries' type 'I'.
endif.

Example: Public Classes


REPORT Z730OOPS26.

*class  lcl_abc  DEFINITION.  "(or)


class lcl_abc DEFINITION create public.
  PUBLIC SECTION.
    methods m1.
endclass.

class lcl_abc IMPLEMENTATION.
  method m1.
    data k type ref to lcl_abc.
    create object k.
  endmethod.
endclass.

class lcl_pqr DEFINITION INHERITING FROM lcl_abc.
  PUBLIC SECTION.
    methods m2.
endclass.

class lcl_pqr IMPLEMENTATION.
  method m2.
    data k type ref to lcl_abc.
    create object k.
  endmethod.
endclass.

class lcl_xyz DEFINITION create protected INHERITING FROM lcl_abc.
endclass.
class lcl_mnr DEFINITION create private INHERITING FROM lcl_abc.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

data ob1 type ref to lcl_pqr.
create object ob1.

Example: Protected Classes


REPORT Z730OOPS27.

class lcl_abc DEFINITION create protected.
  PUBLIC SECTION.
    methods m1.
endclass.

class lcl_abc IMPLEMENTATION.
  method m1.
    data k type ref to lcl_abc.
    create object k.
  endmethod.
endclass.

class lcl_pqr DEFINITION INHERITING FROM lcl_abc.
  PUBLIC SECTION.
    methods m2.
endclass.

class lcl_pqr IMPLEMENTATION.
  method m2.
    data k type ref to lcl_abc.
    create object k.
  endmethod.
endclass.
class lcl_xyz DEFINITION create public INHERITING FROM lcl_abc.
endclass.

class lcl_mnr DEFINITION create private INHERITING FROM lcl_abc.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
*create object ob.  "syntax error
*
data ob1 type ref to lcl_pqr.
*create object ob1.  "syntax error

You might also like