You cannot define a "set.xyz" method in a subclass to set the "xyz" property of the superclass. This restriction prevents a subclass from overriding a superclass implementation and cannot be bypassed. However, in some situations superclass property needs to grant subclasses the ability to participate in the setting or getting of a property value, like in this case. To do so, a superclass "set" function can call a protected method to allow subclasses to participate in property access while still maintaining some level of control.
So the implementation of the two classes might look like this:
classdef MySuperClass < handle
properties
xyz
end
methods
function set.xyz(obj,value)
disp(['setting xyz in MySuperClass to ', value]);
obj.xyz = value;
respondToXYZChange(obj)
end
end
methods(Access = protected)
function respondToXYZChange(obj, value)
end
end
end
classdef MySubClass < MySuperClass
methods
function obj=MySubClass()
disp('!');
end
function update(obj)
disp('updating...');
end
end
methods(Access = protected)
function respondToXYZChange(obj)
obj.update();
end
end
end
>> m = MySubClass3;
!
>> m.xyz = 'a'
setting xyz in MySuperClass to a
updating...
m =
MySubClass3 with properties:
xyz: 'a'
>>
Executing the command:
calls the superclass "set" method which sets the value of the property "xyz" and then calls the protected "respondToXYZChange" which is defined in the subclass to call the "update" method.
Best Answer