Are properties with Access class lists private?

I have the simple class hierarchy below:
classdef myclass
properties (Access = {?ClassA})
p=1;
end
methods
function pdisp(obj)
obj.p
end
end
end
classdef mysubclass<myclass
% properties (Access=public)
% p
% end
methods
function pdisp(obj)
obj.p
end
end
end
In this set-up, it appears the p property is effectively private from an inheritance perspective. We can see that mysubclass does not have access to it,
>> sobj = mysubclass;
>> sobj.pdisp
No public property 'p' for class 'mysubclass'.
Error in mysubclass/pdisp (line 10)
obj.p
^^^^^
The documentation does mention that access to properties is not inherited when a class access list is used,
"Access lists are not inherited, so subclasses of PropertyAccess do not have access to Prop2 unless they explicitly define that access."
But what does it mean here to "explicitly define access"? When a parent class has a private property, it is supposed to be possible to redefine that property in the subclass. However, when I uncomment the property redefinition section in mysubclass, it produces an error.
>> sobj.pdisp
Cannot define property 'p' in class 'mysubclass' because the property has already been defined in the
superclass 'myclass'.

 采纳的回答

Maybe it is because if myclass itself is part of the access list, the property is no longer sealed away from mysubclass --
classdef myclass
properties (Access = {?ClassA,?myclass})
p=1;
end
methods
function pdisp(obj)
obj.p
end
end
end
There are probably ways that these different cases could be detected and managed by Matlab in a way that let's you redefine p when it can't be accessed in subclasses, but maybe they don't want to take on that complexity.

1 个评论

Hi @Catalytic, thanks for your contributions to the MATLAB Central community! I am a UX Researcher here at MathWorks that focuses on our website, and would love to talk with you about your experience, so we can continue to improve. If interested you can contact me directly by clicking my name in this thread and clicking 'Message'. Hope to hear from your soon.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Class Syntax and File Organization 的更多信息

产品

版本

R2024b

提问:

2026-11-1,16:05

评论:

about 6 hours 前

Community Treasure Hunt

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

Start Hunting!

Translated by