inheriting property and changing access
18 views (last 30 days)
Show older comments
I have an abstract class 'TA_Component' with a property 'Length'. in general the propery should be set by the user.
nI also have have a subclass 'TA_rget<TA_Component' that has have a length of 0.
how would I make this happen? i tried to set length to be constant and got the messege "The definition of property 'Length' in class 'TA_rget' differs from its definition in the superclass 'TA_Component'. This is caused by either conflicting access permissions or differing values of the Constant
attribute.
0 Comments
Accepted Answer
Dave B
on 4 Nov 2020
Hi Nathan -
The property can only be defined in one place (i.e. the superclass). It sounds like you want TA_Component to have the Length property without a default value, but TA_rget to have a default value of 0. While you cannot override the default definition in the subclass properties block, you could set this value in the subclass constructor.
For example, if the (abstract) superclass was defined as:
classdef (Abstract) TA_Component
properties
Length;
end
end
And the subclass contained a constructor that set the Length property:
classdef TA_rget < TA_Component
methods
function obj=TA_rget
obj.Length=0;
end
end
end
The result would be that TA_rget's Length is set to 0 on construction (which is very similar to setting the default), but another subclass of TA_Component (which didn't have this custom constructor code) would have a length of empty.
More details on constructor methods can be found at:
3 Comments
Dave B
on 4 Nov 2020
Hi Nathan -
Sorry I missed the Constant bit in your question!
You could force this property to be 0 using a custom set method. This provides an alternative that avoids having to think about whether this property is 'Constant'. Note that 'Constant' means that not only that the property errors when the someone tries to change it, but that it's defined when the class is loaded rather than when an object is instantiated. I think that Constant cannot differ between the superclass and subclass, even for an abstract property. I can think of a few alternatives depending on how you want it to behave (e.g. should it throw an error? or just ignore non-zero values?). Here's an example that errors:
classdef TA_rget < TA_Component
properties
Length=0;
end
methods
function obj=set.Length(obj,newlength)
if newlength~=0
error('Length should only be sepecified as 0!')
end
% No real need to set it here, it's always 0.
obj.Length=0;
end
end
end
More Answers (1)
Steven Lord
on 4 Nov 2020
According to the documentation there are two conditions under which you can redefine superclass properties. I don't believe you can make a property Constant in the subclass if it wasn't already Constant in the superclass or make it non-Constant if it was Constant in the superclass. If you want it to be Constant make it Constant in both superclass and subclass.
2 Comments
Steven Lord
on 4 Nov 2020
You attached TA_rget but not TA_Component, the superclass from which it inherits.
The problem is not that the propery is Abstract in the superclass and not Abstract in the subclass. That's fine.
The problem is (likely) that the property is not Constant in the superclass but Constant in the subclass. My guess at what the relevant section of the superclass looks like:
classdef TA_Component
properties(Abstract)
Length
end
% snip the rest of the class
end
and
classdef TA_rget<TA_Component
% any type of target in a TA system
properties (Constant)
%% unprotected properties
Length=0 % length [m]
N_Sol_Points=1 % number of points in solution
end
% snip the rest of the class
end
Either Length has to be Constant in both classes or Constant in neither class.
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!