I can only speculate as to why it would not be allowed, but I assume there are hazards in using an object that is not finished being constructed...like feeding a baby that is only half way through being born, or driving a car that’s only half assembled.
Below is how I would re-implement. In your original design, it doesn't make sense to me that the classB constructor should have to set x if it has already been set in classA.
classdef superclass
properties
x;
y;
end
end
classdef classA < superclass
properties
a
end
properties (Access=private)
z
end
methods
function obj = classA(a,z,y)
obj.x = a*z;
obj.a = a;
obj.z = z;
if nargin>2
obj.y=y;
end
end
end
end
classdef classB < superclass
properties
b
end
properties (Access=private)
z
end
methods
function obj = classB(b,z,x)
obj.y = z+b;
obj.b = b;
obj.z = z;
if nargin>2
obj.x=x;
end
end
end
end
classdef subclass < classA & classB
methods
function obj = subclass(a,b,z)
obj@classA(a,z);
obj@classB(b,z);
end
end
end
Best Answer