MATLAB: Inheriting property and changing access

classclassesMATLABobjet-orientedoopproperty definitionsubclasssuperclass

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.

Best Answer

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: