Private properties inherited from abstract properties

21 views (last 30 days)
I'm trying to create a general interface with abstract properties, but I don't want them to be public in a subclass. Please have a look at two snippets of my code:
classdef (Abstract) Directivity
properties (Abstract)
info string;
% general information about the directivity object
end
end
classdef RawDirectivity < Directivity
properties (Access = ?Directivity)
info string
% general information about the directivity object
end
end
What I'm trying to achieve is to have every object of a subclass of Directivity to have the 'info' property (initialized in the subclass constructor) that can be then accessed by the superclass (Directivity) methods. Currently, if I try to create an object I get the error:
Error using RawDirectivity
The definition of property 'info' in class 'RawDirectivity' differs from its definition in the superclass 'Directivity'. This is caused by either conflicting access permissions or differing values of the Constant attribute.
If I set property access to public, the error disappears, but I don't want this property to be public, only accessible by a public method.

Accepted Answer

Steven Lord
Steven Lord on 22 Mar 2021
So you only want the superclass and the subclass methods to have access to it? Set its Access, GetAccess, and/or SetAccess attribute to 'protected'. See the entries for those attributes in the documentation for more information.
  2 Comments
Adam Szwajcowski
Adam Szwajcowski on 22 Mar 2021
Isn't Access equivalent to setting both SetAccess and GetAccess to the same value? In which case it is exactly what I did - gave access to the Directivity class. Changing it to 'protected' doesn't solve the problem.
Steven Lord
Steven Lord on 22 Mar 2021
Make the property 'protected' (or give Access to the superclass's metaclass) in both superclass AND subclass. Generally you want the definition of the property to have the same attributes in both superclass and subclass except the superclass also specifies the Abstract attribute.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!