Are properties with Access class lists private?
Show older comments
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 redefintiion 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'.
Answers (1)
Catalytic
on 2 Nov 2026 at 0:08
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.
Categories
Find more on App Building 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!