Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Superclass attributes accessed in subclass

fcousin
Explorer
6,551

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?

8 REPLIES 8
Read only

Sandra_Rossi
Active Contributor
0 Likes
5,131

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?)

Read only

former_member184158
Active Contributor
0 Likes
5,131

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.

Read only

fcousin
Explorer
0 Likes
5,131

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.

Read only

Sandra_Rossi
Active Contributor
0 Likes
5,131

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.

Read only

fcousin
Explorer
0 Likes
5,131

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

Read only

matt
Active Contributor
0 Likes
5,131

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.

Read only

DoanManhQuynh
Active Contributor
0 Likes
5,131

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.

Read only

fcousin
Explorer
0 Likes
5,131

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