inheriting property and changing access
28 views (last 30 days)
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
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
And the subclass contained a constructor that set the Length property:
classdef TA_rget < TA_Component
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:
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.