MATLAB Answers

inheriting property and changing access

23 views (last 30 days)
nathan blanc
nathan blanc on 4 Nov 2020
Commented: nathan blanc on 4 Nov 2020
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.

Accepted Answer

Dave B
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
nathan blanc
nathan blanc on 4 Nov 2020
thanks David, that's a nice workaround!

Sign in to comment.

More Answers (1)

Steven Lord
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
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!