Thread Subject: Using subclass 'get' function to access inherited superclass property

Subject: Using subclass 'get' function to access inherited superclass property

From: Janice

Date: 7 Nov, 2009 19:20:04

Message: 1 of 6

Hi all,

I have read several posts about the problems with overloading a superclass 'set'/'get' function, but I'm interested in simply writing a subclass 'get' function that is able to return an inherited superclass property. It seems that the 'get' function is blind to the superclass properties, despite the fact that one could simply access this property by using dot notation with the subclass object (if the property is public?) elsewhere in the code. The code I'm using is below:

classdef ExptBasic < handle

     properties (GetAccess = 'public', SetAccess = 'protected')
          logfile_name = '';
     end

     methods
          function value = get.logfile_name(Expt)
               value = Expt.logfile_name;
          end
     end

end

classdef TrialBasic < ExptBasic
     methods
          function get.logfile_name(Trial)
               value = get.logfile_name@ExptBasic(Trial);
          end
     end
end

Am I going about this the wrong way? I tried extending the superclass 'get.logfile_name' method in the subclass TrialBasic.

Thanks in advance for the help,
Janice

Subject: Using subclass 'get' function to access inherited superclass property

From: Sven

Date: 12 Nov, 2009 18:37:02

Message: 2 of 6

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 only way I can get around it at the moment is to *remove* the supclass_property from the superclass altogether, which, to me seems like rather poor design.

Thanks,
Sven.

"Janice " <janice_chou@yahoo.com> wrote in message <hd4h94$g23$1@fred.mathworks.com>...
> Hi all,
>
> I have read several posts about the problems with overloading a superclass 'set'/'get' function, but I'm interested in simply writing a subclass 'get' function that is able to return an inherited superclass property. It seems that the 'get' function is blind to the superclass properties, despite the fact that one could simply access this property by using dot notation with the subclass object (if the property is public?) elsewhere in the code. The code I'm using is below:
>
> classdef ExptBasic < handle
>
> properties (GetAccess = 'public', SetAccess = 'protected')
> logfile_name = '';
> end
>
> methods
> function value = get.logfile_name(Expt)
> value = Expt.logfile_name;
> end
> end
>
> end
>
> classdef TrialBasic < ExptBasic
> methods
> function get.logfile_name(Trial)
> value = get.logfile_name@ExptBasic(Trial);
> end
> end
> end
>
> Am I going about this the wrong way? I tried extending the superclass 'get.logfile_name' method in the subclass TrialBasic.
>
> Thanks in advance for the help,
> Janice

Subject: Using subclass 'get' function to access inherited superclass property

From: Matt

Date: 12 Nov, 2009 20:02:18

Message: 3 of 6

"Janice " <janice_chou@yahoo.com> wrote in message <hd4h94$g23$1@fred.mathworks.com>...
> Hi all,
>
> I have read several posts about the problems with overloading a superclass 'set'/'get' function, but I'm interested in simply writing a subclass 'get' function that is able to return an inherited superclass property.
===========

There isn't much of a distinction there. If the superclass doesn't define a get() method, a default get method is used (one that applies no restrictions to the get operation).

=====================
 It seems that the 'get' function is blind to the superclass properties, despite the fact that one could simply access this property by using dot notation with the subclass object (if the property is public?) elsewhere in the code.
==================

No. Any attempt to access the property always implictly calls the 'get' function for that property, including the use of dot notation in the classdef file. The same is true outside the classdef file, unless you've used subsref to overload the dot notation in which case subsref will be called instead. But that doesn't change much - once subsref executes, you'll be in the workspace of the class definition once again where any attempt to access the property calls the get method.



> Am I going about this the wrong way? I tried extending the superclass 'get.logfile_name' method in the subclass TrialBasic.
====

Yes. Just use the superclass' get method, or dot notation. It doesn't appear that you need any more than that.

Subject: Using subclass 'get' function to access inherited superclass property

From: Matt

Date: 12 Nov, 2009 20:06:02

Message: 4 of 6

"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.

Subject: Using subclass 'get' function to access inherited superclass property

From: Matt

Date: 12 Nov, 2009 20:20:09

Message: 5 of 6

"Matt " <xys@whatever.com> wrote in message <hdhpka$4v1$1@fred.mathworks.com>...

> =====================
> It seems that the 'get' function is blind to the superclass properties, despite the fact that one could simply access this property by using dot notation with the subclass object (if the property is public?) elsewhere in the code.
> ==================
>
> No. Any attempt to access the property always implictly calls the 'get' function for that property, including the use of dot notation in the classdef file.
==============

Just to be clear, what I meant here was "if a base class defines a property, then any attempt to access the property always implictly calls a 'get' method defined by the same base class."

Put another way, you cannot avoid the restrictions on property access imposed by a base class' set and get methods.

One thing that might be worth mentioning, though, is that you can still modify the effect of using dot notation to access a property by overloading subsref in the subclass. Within subsref, you still have to access the property subject to the barriers imposed by the superclass get method, but the code for your subsref method can then add new restrictions pertinent to the subclass.

Subject: Using subclass 'get' function to access inherited superclass property

From: Steven Lord

Date: 12 Nov, 2009 21:16:09

Message: 6 of 6


"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
get Janice 7 Nov, 2009 14:24:04
oop Janice 7 Nov, 2009 14:24:04
superclass Janice 7 Nov, 2009 14:24:04
subclass Janice 7 Nov, 2009 14:24:04
inherited prope... Janice 7 Nov, 2009 14:24:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com