Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
OOP: calling constructor in static method

Subject: OOP: calling constructor in static method

From: Watkinrt Watkins

Date: 2 Jun, 2010 21:13:22

Message: 1 of 8

I've run into some problems with the inheritance of a static method for a class I am developing. In the superclass, I have a static method which calls the superclass' constructor. I would then like to define a subclass which inherits the static method from the superclass, but calls the subclass' constructor as apposed to the superclass constructor. Is there anyway to do this? Here is as an overview of what I have:

classdef superclass
    methods
        function obj = superclass()
        end
    end
    methods (Static)
        function obj = staticMethod()
            obj = superclass()
        end
    end
end

classdef subclass < superclass
    methods
        function obj = subclass()
        end
    end
end

If I were to run:
> class(superclass.staticMethod())
>> ans --> superclass
however,
> class(subclass.staticMethod())
>> ans --> superclass

Is there a way to make the static method more generic so that it determines which constructor to call based on the calling class of the static method?

Thanks!

Subject: OOP: calling constructor in static method

From: Matt J

Date: 2 Jun, 2010 21:55:19

Message: 2 of 8

"Watkinrt Watkins" <rhino7890@aol.com> wrote in message <hu6hhi$l0f$1@fred.mathworks.com>...

> Is there a way to make the static method more generic so that it determines which constructor to call based on the calling class of the static method?
========

Why not just overload the static method in the subclass?

Subject: OOP: calling constructor in static method

From: Watkinrt Watkins

Date: 4 Jun, 2010 12:41:34

Message: 3 of 8

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hu6k07$58v$1@fred.mathworks.com>...
> "Watkinrt Watkins" <rhino7890@aol.com> wrote in message <hu6hhi$l0f$1@fred.mathworks.com>...
>
> > Is there a way to make the static method more generic so that it determines which constructor to call based on the calling class of the static method?
> ========
>
> Why not just overload the static method in the subclass?

I could overload the static method, however I would lose the inheritance structure that I was going for. As I have multiple subclasses that will be using this static method, I would prefer to have them simply inherit the method without having to change anything (if it is possible).

I've attempted to use the function mfilename('class') within the static method to determine what the class is of the calling method, but that didn't work; mfilename('class') always returned the value name of the superclass, even when a subclass called the method.

I've also tried to set up the class system as follows:

classdef superclass
    methods
        function obj = superclass()
        end
    end
    methods (Static)
        function obj = staticMethod()
            obj = constructor()
        end
        function obj = constructor()
            obj = superclass()
        end
    end
end

classdef subclass < superclass
    methods
        function obj = subclass()
        end
        function obj = constructor()
            obj = sublcass()
        end
    end
end

however, Matlab gets unhappy with this as the function "constructor" doesn't have any input arguments related to the class (since no instance of the class has been defined yet). If you make the function "constructor" into a static method, you then fall into the same problem as previously described as static methods must be called as "classname.staticMethod()" and there is not easy was to specify the classname.

Does anyone have any other ideas?

Subject: OOP: calling constructor in static method

From: R Wat

Date: 4 Jun, 2010 12:44:05

Message: 4 of 8

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hu6k07$58v$1@fred.mathworks.com>...
> "Watkinrt Watkins" <rhino7890@aol.com> wrote in message <hu6hhi$l0f$1@fred.mathworks.com>...
>
> > Is there a way to make the static method more generic so that it determines which constructor to call based on the calling class of the static method?
> ========
>
> Why not just overload the static method in the subclass?

I could overload the static method, however I would lose the inheritance structure that I was going for. As I have multiple subclasses that will be using this static method, I would prefer to have them simply inherit the method without having to change anything (if it is possible).

I've attempted to use the function mfilename('class') within the static method to determine what the class is of the calling method, but that didn't work; mfilename('class') always returned the value name of the superclass, even when a subclass called the method.

I've also tried to set up the class system as follows:

classdef superclass
    methods
        function obj = superclass()
        end
    end
    methods (Static)
        function obj = staticMethod()
            obj = constructor()
        end
        function obj = constructor()
            obj = superclass()
        end
    end
end

classdef subclass < superclass
    methods
        function obj = subclass()
        end
        function obj = constructor()
            obj = sublcass()
        end
    end
end

however, Matlab gets unhappy with this as the function "constructor" doesn't have any input arguments related to the class (since no instance of the class has been defined yet). If you make the function "constructor" into a static method, you then fall into the same problem as previously described as static methods must be called as "classname.staticMethod()" and there is not easy was to specify the classname.

Does anyone have any other ideas?

Subject: OOP: calling constructor in static method

From: Matt J

Date: 4 Jun, 2010 14:04:06

Message: 5 of 8

"Watkinrt Watkins" <rhino7890@aol.com> wrote in message <huas9u$b6j$1@fred.mathworks.com>...
in the subclass?
>
> I could overload the static method, however I would lose the inheritance structure that I was going for. As I have multiple subclasses that will be using this static method, I would prefer to have them simply inherit the method without having to change anything (if it is possible).
======

Can you explain a little more why you would like to do this? It is highly unusual to want to create an object instance inside a static method. Static methods are intended in fact for situations precisely where you don't need or want to create an object instance. It seems you could accomplish what you're after essentially by writing an external function like the following

function obj=makeObj(ClassName,varargin)

   obj=feval(ClassName,varargin{:})

end

and then

makeObj('superclass')
makeObj('subclass')
etc...

will give you exactly what you're after with essentially the same keystrokes.

Subject: OOP: calling constructor in static method

From: William Payne

Date: 16 Dec, 2010 18:54:08

Message: 6 of 8

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
>"It is highly unusual to want to create an object instance inside a static method."

The "static factory method pattern" uses static methods to construct objects, and to dynamically control the type of the objects that get created, which is not an unusual pattern at all.

Subject: OOP: calling constructor in static method

From: Arnon

Date: 21 Nov, 2013 17:00:16

Message: 7 of 8

"R Wat" wrote in message <hu6hhi$l0f$1@fred.mathworks.com>...
> I've run into some problems with the inheritance of a static method for a class I am developing. In the superclass, I have a static method which calls the superclass' constructor. I would then like to define a subclass which inherits the static method from the superclass, but calls the subclass' constructor as apposed to the superclass constructor. Is there anyway to do this? Here is as an overview of what I have:
>
> classdef superclass
> methods
> function obj = superclass()
> end
> end
> methods (Static)
> function obj = staticMethod()
> obj = superclass()
> end
> end
> end
>
> classdef subclass < superclass
> methods
> function obj = subclass()
> end
> end
> end
>
> If I were to run:
> > class(superclass.staticMethod())
> >> ans --> superclass
> however,
> > class(subclass.staticMethod())
> >> ans --> superclass
>
> Is there a way to make the static method more generic so that it determines which constructor to call based on the calling class of the static method?
>
> Thanks!

Hi,

It's an old thread, however I am also looking for the same and could not find a solution, yet. Is there a way to tell which subclass was used to invoke the supercalss's static method? If you found a solution could you please post it here, or point me to it?

Thank you

Subject: OOP: calling constructor in static method

From: R Wat

Date: 21 Nov, 2013 18:42:13

Message: 8 of 8

"Arnon" wrote in message <l6le70$i4n$1@newscl01ah.mathworks.com>...

>
> Hi,
>
> It's an old thread, however I am also looking for the same and could not find a solution, yet. Is there a way to tell which subclass was used to invoke the supercalss's static method? If you found a solution could you please post it here, or point me to it?
>
> Thank you

It's been a while, but I don't recall ever coming up with a solution to this problem. Sorry I can't be more help.

Tags for this Thread

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.

Contact us