‎2007 Jan 21 1:43 PM
Hello All,
I am defining a class with CREATE PROTECTED addition. My understanding is this class can be instantiated in its subclass. I am using the following code.
CLASS zl_lcl_counter_prot DEFINITION CREATE PROTECTED.
PUBLIC SECTION.
DATA: gv_count_prot TYPE i.
METHODS constructor.
METHODS counter IMPORTING set_value TYPE i
EXPORTING get_value TYPE i.
ENDCLASS.
CLASS zl_lcl_counter_prot IMPLEMENTATION.
METHOD counter.
gv_count_prot = set_value + 1.
get_value = gv_count_prot.
ENDMETHOD. "counter
METHOD constructor.
WRITE / 'Constructor is called'.
ENDMETHOD. "constructor
ENDCLASS. "zl_lcl_counter_prot
CLASS zl_lcl_counter_prot_sub DEFINITION INHERITING FROM zl_lcl_counter_pub.
PUBLIC SECTION.
METHODS create_obj
EXPORTING pointer TYPE REF TO zl_lcl_counter_prot.
ENDCLASS.
CLASS zl_lcl_counter_prot_sub IMPLEMENTATION.
METHOD create_obj.
CREATE OBJECT pointer TYPE zl_lcl_counter_prot.
ENDMETHOD. "create_obj
ENDCLASS.
DATA: z_cnt_prot TYPE REF TO zl_lcl_counter_prot,
z_cnt_prot_sub TYPE REF TO zl_lcl_counter_prot_sub.
START-OF-SELECTION.
CREATE OBJECT z_cnt_prot_sub.
Calling the method of subclass to instantiate the superclass
z_cnt_prot_sub->create_obj( IMPORTING pointer = z_cnt_prot ).
z_cnt_prot->counter( EXPORTING set_value = 1
IMPORTING get_value = gv_counter ).
WRITE: / gv_counter.
I am getting an error that I cant create an instance of the protected superclass in the subclass.
Could anyone please suggest why this error is coming thought I am using CREATE PROTECTED ADDITION.
Regards
Indrajit
‎2007 Jan 21 3:03 PM
When using the extension CREATE PROTECTED, the class can only be instaniated from the methods of the class itself or by the subclasses. So in your case, the zl_lcl_counter_prot_Sub class needs to inherit from zl_lcl_counter_prot. You can change the create_obj method to a static method. See below.
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot DEFINITION CREATE PROTECTED.
PUBLIC SECTION.
DATA: gv_count_prot TYPE i.
METHODS constructor.
METHODS counter IMPORTING set_value TYPE i
EXPORTING get_value TYPE i.
ENDCLASS. "zl_lcl_counter_prot DEFINITION
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot IMPLEMENTATION.
METHOD counter.
gv_count_prot = set_value + 1.
get_value = gv_count_prot.
ENDMETHOD. "counter
METHOD constructor.
WRITE / 'Constructor is called'.
ENDMETHOD. "constructor
ENDCLASS. "zl_lcl_counter_prot
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot_sub DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot_sub DEFINITION INHERITING FROM zl_lcl_counter_prot.
PUBLIC SECTION.
class-METHODS create_obj
EXPORTING pointer TYPE REF TO zl_lcl_counter_prot.
ENDCLASS. "zl_lcl_counter_prot_sub DEFINITION
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot_sub IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot_sub IMPLEMENTATION.
METHOD create_obj.
CREATE OBJECT pointer TYPE zl_lcl_counter_prot.
ENDMETHOD. "create_obj
ENDCLASS. "zl_lcl_counter_prot_sub IMPLEMENTATION
DATA: z_cnt_prot TYPE REF TO zl_lcl_counter_prot,
z_cnt_prot_sub TYPE REF TO zl_lcl_counter_prot_sub.
data: gv_counter type i.
START-OF-SELECTION.
* CREATE OBJECT z_cnt_prot_sub.
* Calling the method of subclass to instantiate the superclass
zl_lcl_counter_prot_sub=>create_obj( IMPORTING pointer = z_cnt_prot ).
z_cnt_prot->counter( EXPORTING set_value = 1
IMPORTING get_value = gv_counter ).
WRITE: / gv_counter.
Regards,
Rich Heilman
‎2007 Jan 21 3:03 PM
When using the extension CREATE PROTECTED, the class can only be instaniated from the methods of the class itself or by the subclasses. So in your case, the zl_lcl_counter_prot_Sub class needs to inherit from zl_lcl_counter_prot. You can change the create_obj method to a static method. See below.
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot DEFINITION CREATE PROTECTED.
PUBLIC SECTION.
DATA: gv_count_prot TYPE i.
METHODS constructor.
METHODS counter IMPORTING set_value TYPE i
EXPORTING get_value TYPE i.
ENDCLASS. "zl_lcl_counter_prot DEFINITION
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot IMPLEMENTATION.
METHOD counter.
gv_count_prot = set_value + 1.
get_value = gv_count_prot.
ENDMETHOD. "counter
METHOD constructor.
WRITE / 'Constructor is called'.
ENDMETHOD. "constructor
ENDCLASS. "zl_lcl_counter_prot
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot_sub DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot_sub DEFINITION INHERITING FROM zl_lcl_counter_prot.
PUBLIC SECTION.
class-METHODS create_obj
EXPORTING pointer TYPE REF TO zl_lcl_counter_prot.
ENDCLASS. "zl_lcl_counter_prot_sub DEFINITION
*----------------------------------------------------------------------*
* CLASS zl_lcl_counter_prot_sub IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS zl_lcl_counter_prot_sub IMPLEMENTATION.
METHOD create_obj.
CREATE OBJECT pointer TYPE zl_lcl_counter_prot.
ENDMETHOD. "create_obj
ENDCLASS. "zl_lcl_counter_prot_sub IMPLEMENTATION
DATA: z_cnt_prot TYPE REF TO zl_lcl_counter_prot,
z_cnt_prot_sub TYPE REF TO zl_lcl_counter_prot_sub.
data: gv_counter type i.
START-OF-SELECTION.
* CREATE OBJECT z_cnt_prot_sub.
* Calling the method of subclass to instantiate the superclass
zl_lcl_counter_prot_sub=>create_obj( IMPORTING pointer = z_cnt_prot ).
z_cnt_prot->counter( EXPORTING set_value = 1
IMPORTING get_value = gv_counter ).
WRITE: / gv_counter.
Regards,
Rich Heilman