2019 May 07 3:08 AM
I am working on a superclass and corresponding subclasses (inheriting the supperclass attributes and methods).
I want to be able to modify the values of the public attributes of the superclass from both the superclass and the subclasses methods as well as external calls.
In essence, I have a superclass and a series of subclasses. I want all of the subclasses to share the superclass attributes and their values. I also want the ability to change the values of the superclass attributes through the methods of the superclass (called within the superclass, the subclasses or externally), the methods of the subclasses (called within the subclasses or externally) as well as outside method calls (i.e. programs).
The updated values (through either methods mentioned above) of the superclass attributes do not seem to be accessible in the subclasses.
Anyone has any recommendations as to how to accomplish this?
I am working on a superclass and corresponding subclasses (inheriting the supperclass attributes and methods).
I want to be able to modify the values of the public attributes of the superclass from both the superclass and the subclasses methods as well as external calls.
In essence, I have a superclass and a series of subclasses. I want all of the subclasses to share the superclass attributes and their values. I also want the ability to change the values of the superclass attributes through the methods of the superclass (called within the superclass, the subclasses or externally), the methods of the subclasses (called within the subclasses or externally) as well as outside method calls (i.e. programs).
The updated values (through either methods mentioned above) of the superclass attributes do not seem to be accessible in the subclasses.
Anyone has any recommendations as to how to accomplish this?
2019 May 07 7:26 AM
No issue to do that, just declare a public or protected attribute in the superclass.
"do not seem to be accessible", what do you mean? (did you use private attributes?)
2019 May 07 7:46 AM
Hi,
I think you can do it without any problem but you must declare the attribute as protected or public. Then you can change the attribute and you can access it from subclass. Just try it.
2019 May 07 11:32 AM
Yes, this is what I thought and have implemented but it doesn't seem to work.
I have defined three classes using the form based version of SE24.
The superclass's attributes are all either public or protected and they do show under the attributes of the two subclasses I have defined (using the "inheriting from superclass" statement).
I then defined a calling program in which I instanciate the superclass and the subclasses. I then call methods from the superclass and the subclasses to modify one of the attributes' value but that value doesn't carry through. Even though I modified an attribute's value in the superclass, the initial value (from the statement
protected section.
data APP_ID type STRING value '00000001' ##NO_TEXT. )
Shows up in the attribute's value in the subclasses. I also tried without an initial value but the subclass's attribute value is empty.
In other words, the attribute's value set in the superclass doesn't seem to be reflected in the corresponding attribute in the subclass.
2019 May 07 12:37 PM
You're wrong in believing that the attribute could "fly" from the superclass instance to the subclass instance. No, the attribute is already inside the subclass instance, you don't need to instantiate the superclass. If you want to make an instance update another instance, you must pass it as a parameter of a method.
2019 May 07 2:25 PM
Thank you Sandra.
Can I call the superclass method with SUPER-> to update the attributes?
So multiple subclasses can’t share data through the superclass attributes?
Thanks
2019 May 07 8:05 PM
No they can't. You just have an instance that can be referred to as typed the same as the superclass. There is no superclass value of an attribute. The value used will always be that of the subclass.
Except when you use a constructor. Then it's a bit more complicated.
2019 May 08 5:50 AM
I think you mixing between class instanciation and implementation. after instance the super class and setting its attributes value, its not automatically implement to subclass. to do this, you could downcast from super class to subclass.
2019 May 09 3:31 PM
Thanks a lot for all the feedback.
I managed to pass data back and forth using constructor and destructor methods in the subclass. I have also defined additional methods in the subclass to update the data (both directions) at any point in the flow.
Regards,
Fred
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |