object oriented MATLAB: static method from parent can’t set protected property from child

Ok, so I have a parent class MOM with a bunch of properties with the SetAccess attribute set to protected. It has a special static method setProperty for setting its properties:

classdef MOM < handle
    properties (SetAccess = 'protected')
        age;
        occupation;
        num_kids;        
        MOMmusthaves ={'age','occupation','num_kids'};
    end
    methods
        function obj = MOM(varargin)            
            if iscell(varargin{1,1})
                varargin =varargin{1,1};
            end
            inputstrings=varargin(cellfun(@isstring,varargin)); % get strings out of the inputs
            if(isempty(setxor(intersect(inputstrings,obj.MOMmusthaves),obj.MOMmusthaves)))
                % if all proper inputs are entered, initialize your object
                for pp =1:length(obj.MOMmusthaves)
                    obj.setProperty(obj,varargin,obj.MOMmusthaves{pp});%find each input in the vararagins, and set it
                end
            else
                display('Hey! These inputs are wrong. type help MOM for an example. Make sure you inlcude inputs for all of the following:');                obj.MOMmusthaves
                return;
            end

        end

    end

    methods (Static)
        function setProperty(obj,varargs,xx)
            eval(sprintf('obj.%s = varargs{find(strcmp(''%s'',varargs))+1};',xx,xx));
        end
    end

end

Then I have a child object KID, which has a couple more properties, also SetAccess protected. When I try to use MOM‘s static method to set a KID property inside the KID‘s constructor I get an error 🙁
The error says:

You cannot set the read-only property ‘allowance’ of KID.

Basically, it seems like KID doesn’t think that it can use MOM‘s static method as its own (so didn’t inherit it properly).

MY QUESTION IS:
Is there any way I can make the static method be recycled and usable to KID for its own protected properties?

JUST FYI, here’s something like the KID code;

classdef KID < MOM
    properties (SetAccess = 'protected')
        gender;
        allowance;
        favoritecandy;
        KIDmusthaves ={'gender','allowance','favoritecandy'};
    end
    methods
        function obj = KID(varargin)

            obj = obj@MOM(varargin(:)); % Special construct for instantiating the superclass
            inputstrings=varargin(cellfun(@isstring,varargin)); % get strings out of the inputs.

            if(isempty(setxor(intersect(inputstrings,obj.KIDmusthaves),obj.KIDmusthaves)))
                % if all proper inputs are entered, initialize your object
                for pp =1:length(obj.KIDmusthaves)
                    %find each input in the vararagins, and set it
                    obj.setProperty(obj,varargin,obj.KIDmusthaves{pp});
                end
            else
                display('please use correct input format. Make sure you include inputs for all of the following:');
                obj.KIDmusthaves
                return;
            end

        end

    end
end


Source: oop

Leave a Reply