Thread Subject: Superclass methods using constant subclass properties?

Subject: Superclass methods using constant subclass properties?

From: Chris Hall

Date: 20 Nov, 2009 05:12:20

Message: 1 of 7

I am making a bunch of similar classes, to simplify things I have made a superclass with abstract properties that are common to all of the classes, the classes then features from the superclass particularly methods. My classes have a lot of constants and a lot of methods that would make good static methods, but I can't figure out how to use my constant subclass properties in a superclass static method, perhaps an example

classdef Country
    properties (Abstract)
        population
        king
    end
    methods
        function Kingspower(obj)
            fprintf('The king %s rules over %d people\n', obj.king, ...
                obj.population);
        end
        function obj = Country()
        end
    end
end

classdef SomeCountry < Country
    properties
        population = 1000000;
        king = 'Sir Edward';
    end
    methods
        function obj = SomeCountry()
        end
    end
end

now we use the classes
k = SomeCountry
k.Kingspower()
The king Sir Edward rules over 1000000 people
As written the function Kingspower writes out the population and king from SomeCountry. What I would like to do is change the properties in some country to constant and change the method Kingspower to static so the function could be called as follows:
SomeCountry.Kingspower
which would then return an identical response, then I would like to be able to do the same with another subclass
SomeOtherCountry.Kingspower
The king Some Other King rules over 500 people
Does anyone know how to make it so that I don't have to instantiate the properties I'm using or the methods?
Thanks a bunch,
Chris.

Subject: Superclass methods using constant subclass properties?

From: Chris Hall

Date: 24 Nov, 2009 15:42:17

Message: 2 of 7

To make what I'm trying to do a little more explicit, I have a bunch of lookup tables with values and headers for the tables that don't change for table, but there are many different tables which I am defining as different classes. There are a set of methods that are common to all of these tables, although I need to make slight variations to some of the methods depending on the table.

Using constant properties, I can directly access my tables without instantiating an instance of the class, ie
table1.value(3,4)

If I don't use constant properties than I can instantiate my table and use static methods from the superclass which can then use my table of values, ie
obj = table1()
obj.superclass_method1()

I would like the superclass methods to be static and the subclass properties to be constant, so that I do not need to instantiate anything, but I will still have access to my methods and constants, ie
table1.superclass_method1()

but I can't figure out the syntax to make that happen. It doesn't like my definition in the superclass of an Abstract Constant property, is this allowed?

Much thanks,
Chris.

Subject: Superclass methods using constant subclass properties?

From: Matt

Date: 24 Nov, 2009 17:49:18

Message: 3 of 7

"Chris Hall" <chris.hall@aggiemail.usu.edu> wrote in message <he58fk$9lf$1@fred.mathworks.com>...

> As written the function Kingspower writes out the population and king from SomeCountry. What I would like to do is change the properties in some country to constant and change the method Kingspower to static so the function could be called as follows:
> SomeCountry.Kingspower
==============================

Not possible, I don't think, at least not by any means that would make the effort worthwhile.

A static method cannot access the properties of the class, because for that it requires either an instance of that class or (if the property is constant) the class name. The latter would not be available to a static method in the superclass.

The closest thing I came up with is to have static methods both in the superclass and the subclass as follows:

classdef Country

    methods (Static)
        function Kingspower(king,pop)
            fprintf('The king %s rules over %d people\n', king, pop);
        end
    end
    
    methods
        function obj = Country()
        end
    end
end



classdef SomeCountry < Country
    properties (Constant)
        population = 1000000;
        king = 'Sir Edward';
    end
    methods (Static)
        function Kingspower
           pop=eval([mfilename,'.population']);
           king=eval([mfilename,'.king']);
           Country.Kingspower(king,pop);
        end
        
    end
    methods
        function obj = SomeCountry()
        end
    end
end





>> SomeCountry.Kingspower
The king Sir Edward rules over 1000000 people

Subject: Superclass methods using constant subclass properties?

From: Matt

Date: 24 Nov, 2009 18:15:23

Message: 4 of 7

"Chris Hall" <brainchild399@yahoo.com> wrote in message <hegusp$9ho$1@fred.mathworks.com>...

> If I don't use constant properties than I can instantiate my table and use static methods from the superclass which can then use my table of values, ie
> obj = table1()
> obj.superclass_method1()
>
> I would like the superclass methods to be static and the subclass properties to be constant, so that I do not need to instantiate anything,
===========================

Just out of curiousity, why is it a big deal just to instantiate a class?

Subject: Superclass methods using constant subclass properties?

From: Chris Hall

Date: 24 Nov, 2009 19:35:13

Message: 5 of 7

>
> Just out of curiousity, why is it a big deal just to instantiate a class?

Thanks for the help. I just have a whole bunch of different classes and might only use a few at a given time, so I was trying to avoid having to instantiate a few random classes use their functions and then instantiate a few more and use a few more data points. It just seemed more convenient and to make sense to not have to manually instantiate my classes, I guess I can write a script to instantiate them all and then just use the ones I need.
Thanks,
Chris.

Subject: Superclass methods using constant subclass properties?

From: Matt

Date: 25 Nov, 2009 11:24:03

Message: 6 of 7

"Chris Hall" <brainchild399@yahoo.com> wrote in message <hegusp$9ho$1@fred.mathworks.com>...
> To make what I'm trying to do a little more explicit, I have a bunch of lookup tables with values and headers for the tables that don't change for table, but there are many different tables which I am defining as different classes. There are a set of methods that are common to all of these tables, although I need to make slight variations to some of the methods depending on the table.
>
> Using constant properties, I can directly access my tables without instantiating an instance of the class, ie
> table1.value(3,4)
>
> If I don't use constant properties than I can instantiate my table and use static methods from the superclass which can then use my table of values, ie
> obj = table1()
> obj.superclass_method1()
=================

The example you've shown here is not that of a static method call. It's a call to an ordinary class method.


>
> I would like the superclass methods to be static and the subclass properties to be constant, so that I do not need to instantiate anything, but I will still have access to my methods and constants, ie
> table1.superclass_method1()
>
> but I can't figure out the syntax to make that happen. It doesn't like my definition in the superclass of an Abstract Constant property, is this allowed?
========================

It doesn't really seem necessary that the properties be Abstract for what you're trying to do. Since each subclass will always possess a table property anyway, you may as well introduce the property only in the subclass.

It might also be worth pointing out that static methods aren't the only way to access class data without instantiating the class. You can also use normal, external functions as well. Consider the following modification of the examples we've been working with:


classdef Country %Superclass - empty here, but in general it doesn't have to be
end

classdef SomeCountry < Country
    properties (Constant)
        population = 1000000;
        king = 'Sir Edward';
    end
end

classdef SomeOtherCountry < Country
    properties (Constant)
        population = 500;
        king = 'Agamenon';
    end
end


function Kingspower(ClassName) %an ordinary external function


   pop=eval([ClassName,'.population']);
   king=eval([ClassName,'.king']);
   
   fprintf('The king %s rules over %d people\n', king, pop);

end
%%%%%%%%%%


We can now do the following with essentially the same syntax simplicity as what you were trying to get from static methods:

>> Kingspower SomeCountry
The king Sir Edward rules over 1000000 people

>> Kingspower SomeOtherCountry
The king Agamenon rules over 500 people

I guess the ugly thing about this solution is that it uses eval(), which tends to come under criticism a lot, but hey, it gets the job done...

Subject: Superclass methods using constant subclass properties?

From: Matt

Date: 25 Nov, 2009 12:22:05

Message: 7 of 7

"Chris Hall" <brainchild399@yahoo.com> wrote in message <hegusp$9ho$1@fred.mathworks.com>...

> It doesn't like my definition in the superclass of an Abstract Constant property, is this allowed?
===============

Yes, it's allowed, but if a superclass property is declared as both Abstract and Constant, the subclass version of the property must be declared as Constant.

This only solves half of your problem, however. You can make the properties both Abstract in the superclass and Constant everywhere else, but making constant properties accessible by a function (static or otherwise) still requires that you pass the name of the subclass to that function somehow.

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
oop Matt J 24 Nov, 2009 14:48:50
methods Chris Hall 20 Nov, 2009 00:14:06
constant Chris Hall 20 Nov, 2009 00:14:06
static Chris Hall 20 Nov, 2009 00:14:06
subclass Chris Hall 20 Nov, 2009 00:14:06
properties Chris Hall 20 Nov, 2009 00:14:06
objectoriented Chris Hall 20 Nov, 2009 00:14:06
superclass Chris Hall 20 Nov, 2009 00:14:06
rssFeed for this Thread

Contact us at files@mathworks.com