tags:

views:

159

answers:

1

Question about subclassing in matlab, under the new class system. I've got class A with some protected properties:

classdef Table < Base

properties (SetAccess = protected, GetAccess = public)
    PropA = [];
end %properties

I'd like to make a subclass with some specialized features, and further restrict access to PropA. (i.e. make get access private in the subclass). My first thought was:

classdef subTable < Table

...
methods (Access = private)
    out = get.PropA(obj, value);
end %private methods

However, in the help it says: "You must define property access methods in a methods block that specifies no attributes." So much for that idea.

Any ideas?

+2  A: 

I don't believe this is possible. From MATLAB Documentation:

There are only two conditions that allow you to redefine superclass properties:

  • The superclass property Abstract attribute is set to true
  • The superclass property has both the SetAccess and GetAccess attributes set to private

Nor do I think that doing this would be a good idea. It violates the Liskov Substitution Principle. Functions written to accept a Table should also be able to accept a subTable and work properly. If such a function accessed PropA, it would fail when passed a subTable.

SCFrench