Why is it not possible to change access attribute of methods in subclasses?

7 views (last 30 days)
If I'm not wrong, it is not possible to change the access attribute of a superclass' method when it is overridden in a subclass.
Indeed, a method that is 'protected' in a superclass should never become 'public' in a subclass. However, it can be useful to make 'protected' or 'private' a method that was 'public' in the superclass.
In other words, I would like to restrict access to a method in the subclass, while still letting the subclass itself use the method for its internal code (as for protected and private methods).
Is there a way to do that?
As an example of application, suppose we have made the class DynamicTree, which represents a tree data structure. The class has the method 'changeParent', which allows the user to change the parent of a node, thus moving the corresponding sub-tree somewhere else on the tree (this is why "DynamicTree").
Now I want to make the subclass StaticTree, derived from DynamicTree, in which sub-trees can still be moved around, but according to some mechanism internal to the subclass. Therefore, the subclass needs the method 'changeParent', but I don't want users to be able to change parents at their will.
A possible solution would be to override 'changeParent' in StaticTree, including in it all the mechanism required by the subclass. But this can be really painful in some cases; and it is in mine!
Another solution can be something like this:
classdef DynamicTree
% ... some code ...
method (Access=public)
function changeParent(obj, newparent)
if strcmp(class(obj), 'DynamicTree')
% obj does not belong to a subclass;
% do change the parent.
obj.doChangeParent(obj, newparent)
else
% obj belongs to a subclass;
error('you cannot do that!')
end
end
end
method (Access=protected)
function doChangeParent(obj, newparent)
% ...here goes the specific code...
end
end
end
Then class StaticTree will use "doChangeParent" for its needs, and maybe override method 'changeParent' with a 'Hidden' wrapper.
Quite cumbersome and inelegant indeed.
Wouldn't it be much easier if one could just override 'changeParent' with a wrapper with attribute 'Access = private'?
Does anyone have some suggestion?
Thanks.

Answers (1)

Jim Hokanson
Jim Hokanson on 10 Aug 2013
I realize at this point this is an old post but I would like to have a solution like this. In my case I wanted to be able to make addprop of the dynamicprops class private. I'd prefer to have a method for explicitly defining the change in attributes with no other changes but as far as I know this is not possible. A common workaround or perhaps the accepted approach is to do the following:
class StaticTree
method (Access=private)
function changeParent(varargin)
changeParent@DynamicTree(varargin{:})
end
end
end
The basic idea is to just call the superclass in whatever way is easiest. Typically this will involve the use of varargin on the input. You could also potentially use varargout to generalize the output. It's a few more lines of code than I would like but I think it is the only thing you can do.

Categories

Find more on Properties 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!