10-09-2007 7:52 AM
Hello Gurus,
New to OOPS and stuck now. So need your help.
Have created two classes, one super class and one sub class.
Sub class has one extra method.
Created references for both classes.
Now i tried refsubclass ?= refsuperclass.
Its dumping.
Wat may be the cause.
Help pls.
Rgds,
Renjith
10-09-2007 8:00 AM
10-09-2007 8:07 AM
Hi,
this "refsubclass ?= refsuperclass" must dumping if the "refsuperclass" point to an instance of the superclass.
Only something like this will work:
create object refsuperclass type <subclasstype>.
Now the statement work.
refsubclass ?= refsuperclass (this is the widening cast).
This work too:
create object refsubclass type <subclasstype>.
refsuperclass = refsubclass (this is narrowing cast).
But this will not work and dump:
create object refsuperclass type <superclasstype>.
refsubclass ?= refsuperclass
Regards,
Gianpietro
10-09-2007 8:16 AM
Hi,
Try this way.
CLASS lcl_employee DEFINITION.
PUBLIC SECTION.
*----
The public section is accesible from outside
*----
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
END OF t_employee.
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_employees.
PROTECTED SECTION.
*----
The protecetd section is accesible from the class and its subclasses
*----
Class data are global for all instances
CLASS-DATA: g_no_of_employees TYPE i.
PRIVATE SECTION.
*----
The private section is only accesible from within the classs
*----
DATA: g_employee TYPE t_employee.
ENDCLASS.
*--- LCL Employee - Implementation
CLASS lcl_employee IMPLEMENTATION.
METHOD constructor.
g_employee-no = im_employee_no.
g_employee-name = im_employee_name.
g_no_of_employees = g_no_of_employees + 1.
ENDMETHOD.
METHOD display_employee.
WRITE:/ 'Employee', g_employee-no, g_employee-name.
ENDMETHOD.
METHOD display_no_of_employees.
WRITE: / 'Number of employees is:', g_no_of_employees.
ENDMETHOD.
ENDCLASS.
************************************************************************
R E P O R T
*********************************************************************
DATA: g_employee1 TYPE REF TO lcl_employee,
g_employee2 TYPE REF TO lcl_employee.
START-OF-SELECTION.
CREATE OBJECT g_employee1
EXPORTING im_employee_no = 1
im_employee_name = 'Vikram.C'.
CREATE OBJECT g_employee2
EXPORTING im_employee_no = 2
im_employee_name = 'Raghava.V'.
CALL METHOD g_employee1->display_employee.
CALL METHOD g_employee2->display_employee.
Thanks,
Anitha
10-12-2007 11:15 AM
Hi
<b>Synatx and Visisbilty</b>
1)the public and protected components of the super class are visible in the subclass
2)private section of the super class is not visible in the sub classes
3)private components of the sub class can have same name as the private component of the super class
4) public and protected components of the sub class can not have the same name as that have been used in super class
<b>Synatx</b>
CLASS SUPER_CLAS DEFINATION
PUBLIC SECTION.
PUBLIC COMPONENTS
PROTECTED SECTION
PROTECTED COMPONETS
PRIVATE SECTION
PRIVATE COMPONENTS
ENDCLASS
CLASS SUB_CLASS DEFINATION INHERITING FROM SUPER_CLASS
PUBLIC SECTION.
PUBLIC COMPONENTS
PUBLIC COMPONENTS (SUPER_CLASS)
PROTECTED SECTION
PROTECTED COMPONETS
PROTECTED COMPONENTS (SUPER CLASS)
PRIVATE SECTION
PRIVATE COMPONENTS
ENDCLASS.
<b>friend class</b>
CLASS C1 DEFINITION FRIENDS OBJ1 .. OBJN
CLASS C1 DEFINITION LOCAL FRIENDS OBJ1..OBJN
CLASS C1 DEFINITION GLOBAL FRIENDS OBJ1..OBJN
with the friend relationship one class can grant another class or interface (and all clases that implement that interface ) acess to its PROTECTED AND PRIVATE COMPONENTS
friendship is one sided class c1 granting feindship to class c2 does not mean c1 is a friend of c2 unless c2 explicitly grants c1 as friend
subclasses of friend and interfaces that receives a friend as a component interface
become friend
a friend of a super class is not automatically friend of itssub class
<b>Reward if useful</b>