How do I update an inherited property in both the SuperClass and SubClass simultaneously in real time?

11 views (last 30 days)
I have a property (Observation) which is a vector, of which different elements are updated by the super class and the sub class.
Here is a simplified example of what I'm trying to do:
classdef TestSuperClass < handle
properties
Observation
end
methods
function obj = TestSuperClass()
obj.Observation = zeros(1,2);
end
function obj = TestSuperMethod1(obj,value)
obj.Observation(1) = value;
disp(obj.Observation)
end
end
end
SubClass:
classdef TestSubClass < TestSuperClass
properties
end
methods
function obj = TestSubClass()
end
function obj = TestSubMethod1(obj,data)
obj.Observation(2) = data;
disp(obj.Observation)
end
end
end
Here is the "Main" function:
function TestMain()
SUP = TestSuperClass();
SUB = TestSubClass();
SUP
for i = [11 22 33]
TestSuperMethod1(SUP,i);
end
SUB
for j = [99 88 77]
TestSubMethod1(SUB,j);
end
end
Here are the results
>> TestMain
SUP =
TestSuperClass with properties:
Observation: [0 0]
11 0
22 0
33 0
SUB =
TestSubClass with properties:
Observation: [0 0]
0 99
0 88
0 77
I was expecting the inherited value of Observation to be updated as follows:
33 99
33 88
33 77
Here are the links to the documentation I have been using:
In the documentation "Comparison of Handle and Value Classes" there are these sections:
User-Defined Handle Classes
Instances of classes that derive from the handle class are references to the underlying object data. When you copy a handle object, MATLAB copies the handle, but does not copy the data stored in the object properties. The copy refers to the same object as the original handle. If you change a property value on the original object, the copied handle references the same change.
(It then goes on to show an example of "Handle Object Behavior" which would appear to be exactly what I'm trying to do).
Modifying Handle Objects in Functions
When you pass a handle object to a function, MATLAB creates a copy of the handle in the function workspace. Because copies of handles reference the same underlying object, functions that modify the handle object effectively modify the object in the caller’s workspace as well. Therefore, it is not necessary for functions that modify handle objects passed as input arguments to return the modified object to the caller.
For more information, see Object Modification.
In the documentation "Comparison of MATLAB and Other OO Languages" there is this section:
Object Modification
MATLAB classes can define public properties, which you can modify by explicitly assigning values to those properties on a given instance of the class. However, only classes derived from the handle class exhibit reference behavior. Modifying a property value on an instance of a value classes (classes not derived from handle), changes the value only within the context in which the modification is made.
The sections that follow describe this behavior in more detail.Objects Passed to Functions
MATLAB passes all variables by value. When you pass an object to a function, MATLAB copies the value from the caller into the parameter variable in the called function.
However, MATLAB supports two kinds of classes that behave differently when copied:
  • Handle classes — a handle class instance variable refers to an object. A copy of a handle class instance variable refers to the same object as the original variable. If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and copied handles.
  • Value classes — the property data in an instance of a value class are independent of the property data in copies of that instance (although, a value class property could contain a handle). A function can modify a value object that is passed as an input argument, but this modification does not affect the original object.
See Comparison of Handle and Value Classes for more information on the behavior and use of both kinds of classes.
In the documentation "Handle Object Behavior" there is this section:
Handle Objects Modified in Functions
When you pass an argument to a function, the function copies the variable from the workspace in which you call the function into the parameter variable in the function’s workspace.
Passing a nonhandle variable to a function does not affect the original variable that is in the caller’s workspace. For example, myFunc modifies a local variable called var, but when the function ends, the local variable var no longer exists:
(There is an example and then the following paragraph:)
When the argument is a handle variable, the function copies only the handle, not the object identified by that handle. Both handles (original and local copy) refer to the same object.
When the function modifies the data referred to by the object handle, those changes are accessible from the handle variable in the calling workspace without the need to return the modified object.
Conclusion:
If I understand the documentation above, then my simple example above should work as I expect.
I must be missing something simple, but I can't see it. Any help would be most appreciated.

Accepted Answer

Jeff Miller
Jeff Miller on 16 Oct 2022
The superclass/subclass relationship is a little different than you think. When you make SUP and SUB, these are completely different objects. Each has its own Observation property, but those are tied together in any way. It's the same as if you had two structures s1 and s2, each with a field called Observation. s1.Observation and s2.Observation would refer to completely different things in memory. What inheritance buys you is the ability to write things like
TestSuperMethod1(SUB,i);
  5 Comments
Gary
Gary on 17 Oct 2022
Jeff,
Given your insights, I tried a couple of alternatives, and I think I achieved what I wanted to:
% Superclass call the subclass method
for k = [5 6 7]
SUP = TestSuperMethod1(SUB,k);
pause(1);
end
disp(' ');
% Subclass call the superclass method
for m = [12 23 34]
SUB = TestSubMethod1(SUP,m);
pause(1);
end
When I run the TestMain function again, I get the following results:
>> TestMain
11 0
22 0
33 0
0 99
0 88
0 77
5 77
6 77
7 77
7 12
7 23
7 34
>>
Now I have to figure out how to apply this principle to my other code.
Thanks again.

Sign in to comment.

More Answers (0)

Categories

Find more on Handle Classes in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!