Reputation: 5211
I have the following problem. I have three classes: A, B, and C. A is the base class, B inherits A, and C inherits B.
Each has a constructor that takes in alot of arguments. However, the constructor for B does a few steps that I don't want in C that I can't undo unless I add more interface code, which would break encapsulation for a few variables. Namely, I'd give the user of my class the ability to change a few variables that I don't want them to.
As a result, I thought that I'd be clever and try to call the constructor of A from C. However MATLAB doesn't like this. See code below.
classdef C < B
% properties go here
% ...
methods(Access = public)
function obj = C(arguments)
obj = obj@A(A's arguments); % MATLAB doesn't like this
% ...
end
end
So, how can I (or can I not), call the constructor to A?
Upvotes: 1
Views: 264
Reputation: 6060
the constructor for B does a few steps that I don't want in C
In that case, your C is not an B. If the constructor from B is not executed in the construction process of C, then C cannot be a valid object of class B. However, the inheritance relationship implies this.
As such, the inheritance C < B is wrong. You should inherit C from A.
If you really want to, I'd try to implement a (mostly empty) protected constructor in B that is then called from C. Not entirely sure that works in Matlab though.
Upvotes: 2