Why is calling a superclass constructor after a use of the object not allowed?
I have a subclass “subclass” of two classes “classA” and “classB”, which are in turn subclasses of “superclass”. Here is the class tree:
The superclass has two properties x and y. The classes “classA” and “classB” each do something special to initialize x and y, respectively. Then I want “subclass” to call both the “classA” and “classB” constructors to initialize x and y simultaneously. But the way the classes are built, I run into a snag.
Here are the definitions of the classes:
superclass.m:
classdef superclass
%% PROPERTIES
properties
x;
y;
end
%% METHODS
methods
function obj = superclass(x,y)
% Initialize the properties
obj.x = x;
obj.y = y;
end
end
end
classA.m:
classdef classA < superclass
%% PROPERTIES
properties
a;
end
%% METHODS
methods
% Constructor
function obj = classA(a,y,z)
x = a*z;
obj@superclass(x,y);
obj.a = a;
obj.z = z;
end
end
end
classB.m:
classdef classB < superclass
%% PROPERTIES
properties
b;
end
%% METHODS
methods
% Constructor
function obj = classB(x,b,z)
y = z+b;
obj@superclass(x,y);
obj.b = b;
obj.z = z;
end
end
end
subclass.m:
classdef subclass < classA & classB
%% METHODS
methods
% Constructor
function obj = subclass(a,b,z)
obj@classA(a,0,z);
obj@classB(obj.x,b,z); % Why is this not allowed and how do I work around that
% without redoing the calculations done in classes "classA" and "classB"?
end
end
end
This gives the following error message:
"A constructor call to superclass classB appears after the object is used, or after a return."
A couple of questions:
1) Why is calling a superclass constructor after a use of the object not allowed?
2) How do I work around that without redoing the calculations done in classA and classB? I feel like I might to redesign the class hierarchy. But it just bugs me that it can’t be done like above.
I use Matlab R2019a on Windows 10.
NOTE:-
Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.
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
properties
x;
y;
end
end
classdef classA < superclass
%% PROPERTIES
properties
a
end
SEE COMPLETE ANSWER CLICK THE LINK