Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Using subclass 'get' function to access inherited superclass property
Date: Thu, 12 Nov 2009 16:16:09 -0500
Organization: The MathWorks, Inc.
Lines: 91
Message-ID: <hdhtsr$2k1$1@fred.mathworks.com>
References: <hd4h94$g23$1@fred.mathworks.com> <hdhkke$m7a$1@fred.mathworks.com> <hdhpra$ihm$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1258060507 2689 172.31.44.65 (12 Nov 2009 21:15:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 12 Nov 2009 21:15:07 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:584695



"Matt " <xys@whatever.com> wrote in message 
news:hdhpra$ihm$1@fred.mathworks.com...
> "Sven" <sven.holcombe@gmail.deleteme.com> wrote in message 
> <hdhkke$m7a$1@fred.mathworks.com>...
>> I have the exact same conundrum. If anyone knows an answer (even if that 
>> answer is "MATLAB can't do this in the current version"), please post it 
>> here.
>>
>> When I add a get.supclass_property(this) method to my subclass, MATLAB 
>> tells me:
>> "Cannot specify a get function for property 'supclass_property' in class 
>> 'subclass', because that property is not defined by that class.
>>
>> When I go ahead and *add* supclass_property as a property of my subclass, 
>> MATLAB tells me:
>> "Cannot define property 'supclass_property' in class 'subclass' because 
>> the property has already been defined in the super-class 'supclass'.
> =====
>
> The idea is you're supposed to be defining get/set methods for properties 
> only in the class that defines that property. If it were possible to 
> add/overload get and set methods in a subclass, it would be possible to 
> set that property in the subclass with a value that is illegal from the 
> point of view of the superclass. Then the superclass' methods would not 
> work anymore.

That's correct.  You could _add_ restrictions by having the superclass's 
set.<property> method call another validation method that the subclass 
overloads, but you can't _relax_ restrictions in the subclass by overloading 
the property set method.  Remember, though, that only the set.<property> 
method is allowed to actually set the property; any of the "helper" 
functions that you want to use to perform validation can change the value 
that you're validating but not the property itself.  Thus even though I 
called otherValidation with the object as input, that's just so we can 
invoke the overloaded otherValidation from the subclass.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brgsek9-1.html

z1 = evenNumbers(4)
z2 = multipleOfFour(4)
z3 = evenNumbers(6)
z4 = multipleOfFour(6)
z5 = evenNumbers(3)
z6 = multipleOfFour(3)


% begin evenNumbers.m
classdef evenNumbers
    properties
        value = 0;
    end
    methods
        function obj = evenNumbers(x)
            obj.value = x;
        end
        function obj = set.value(obj, newvalue)
            if mod(newvalue, 2) == 1
                error('evenNumbers:noOddValues', 'evenNumbers only accepts 
even values for its property ''value''.');
            end
            newvalue = otherValidation(obj, newvalue)
            obj.value = newvalue;
        end
        function newvalue = otherValidation(obj, newvalue)
            % this does nothing in evenNumbers
        end
    end
end

% begin multipleOfFour.m
classdef multipleOfFour < evenNumbers
    methods
        function obj = multipleOfFour(x)
            obj@evenNumbers(x);
        end
        function newvalue = otherValidation(obj, newvalue)
            if mod(newvalue, 4) ~= 0
                error('multipleOfFour:nonMultiple', 'multipleOfFour only 
accepts multiples of 4 as its ''value''.');
            end
        end
    end
end

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ