Private properties inherited from abstract properties
21 views (last 30 days)
Show older comments
Adam Szwajcowski
on 22 Mar 2021
Commented: Steven Lord
on 22 Mar 2021
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.
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Subclass Definition 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!