Thank you, but I should have rephrased my question - I was looking for a way to overwrite the value set by the superclass constructor.
I'm not sure why it would be preferable to overwrite when you can just set the value in the subclass constructor directly. Nevertheless, if you must do this, an alternative would be to convert your original immutable property (let's call it "immutableProp") to a Dependent property like in the following,
classdef myclass
properties (SetAccess=immutable)
p;
end
properties (Dependent)
immutableProp;
end
methods
function obj=myclass(pValue)
obj.p=pValue;
end
function out=get.immutableProp(obj)
out = obj.accessIt;
end
function out=accessIt(obj)
out=obj.p;
end
end
end
Then in your subclass, you simply overload accessIt() in a way that causes immutableProp to refer to q instead of p.
classdef mysubclass<myclass
properties (SetAccess=immutable)
q;
end
methods
function obj=mysubclass(pValue, qValue)
obj=obj@myclass(pValue);
obj.q=qValue;
end
function out=accessIt(obj)
out=obj.q;
end
end
end
Best Answer