2010 Oct 12 10:15 AM
Hello Xperts,
assuming that there is a class hierarchy with an abstract super class and sub-classes sub-class1 & sub-class2 .
Another class (handler of annonymous objects through a list in an ITAB) has the following declaration:
types: begin of ty_catline,
name type string,
ref type ref to super-class,
end of ty_catline,
ty_t_cat type standard table of ty_catline. "list with the objects sub-class1 OR sub-class2
Will that work?
If not, is there a way to make the declaration such that the list can contain either sub-class1 OR sub-class2 objects.
Or must a separate type be declared for a list of each sub-class?
Thanx in advance.
2010 Oct 12 10:34 AM
This is correct!
A super class is more general than its sub classes so using reference of the former you can store objects of the latter. So the valid is
data wa_catline type ty_catline.
create object wa_catline-ref type subclass1.
"which is the same as
data lr_subclass2 type ref to ...
create object lr_subclass2.
wa_catline-ref = lr_subclass2.
The problematic would be holding supreclass type object in subclass object reference. But in your case up cast takes place, which is easy to handle.
Regards
Marcin
PS: In the real world, the CAR or SHIP is a VEHICLE, but not essentialy vice versa (i.e.VEHICLE can be PLANE). Here vehicle is super class, while car and ship are sub classes. So you can always make assignment in the way super class = sub class.
Sorry for trival example but it shows the idea:)
Edited by: Marcin Pciak on Oct 12, 2010 11:34 AM
Hello Xperts,
assuming that there is a class hierarchy with an abstract super class and sub-classes sub-class1 & sub-class2 .
Another class (handler of annonymous objects through a list in an ITAB) has the following declaration:
types: begin of ty_catline,
name type string,
ref type ref to super-class,
end of ty_catline,
ty_t_cat type standard table of ty_catline. "list with the objects sub-class1 OR sub-class2
Will that work?
If not, is there a way to make the declaration such that the list can contain either sub-class1 OR sub-class2 objects.
Or must a separate type be declared for a list of each sub-class?
Thanx in advance.
2010 Oct 12 10:34 AM
This is correct!
A super class is more general than its sub classes so using reference of the former you can store objects of the latter. So the valid is
data wa_catline type ty_catline.
create object wa_catline-ref type subclass1.
"which is the same as
data lr_subclass2 type ref to ...
create object lr_subclass2.
wa_catline-ref = lr_subclass2.
The problematic would be holding supreclass type object in subclass object reference. But in your case up cast takes place, which is easy to handle.
Regards
Marcin
PS: In the real world, the CAR or SHIP is a VEHICLE, but not essentialy vice versa (i.e.VEHICLE can be PLANE). Here vehicle is super class, while car and ship are sub classes. So you can always make assignment in the way super class = sub class.
Sorry for trival example but it shows the idea:)
Edited by: Marcin Pciak on Oct 12, 2010 11:34 AM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |