MATLAB Answers


Call superclass method without knowing name of superclass?

Asked by Jared
on 20 Jul 2011
Latest activity Edited by per isakson
on 29 Apr 2018
I would like to call a method of a superclass from a subclass after performing some additional operations within the subclass method. However, to make the code general (say I change the superclass name or add an intermediate class later between the super- and subclass), I would like to be able to call the method without explicitly naming the superclass, which would prevent me from using the standard command:
I have thought of two ways of doing this, neither of which I am enamored of.
First, use an eval statement:
eval(['function_name@' superclass_name '(arg1,arg2,etc)'])
Second, create a "glue class", which subclasses the actual class of interest and contains nothing more than a constructor:
classdef glue_class < superclass_of_interest
function obj = glue_class(varargin)
obj = obj@superclass_of_interest(varargin{:});
Now if I subclass the glue_class instead of the superclass_of_interest, I only need to change the class name within a the (simple) glue_demo file instead of in every function of my actual class from which I would like to call a superclass method.
Is there a more elegant solution? My naive attempts at creating function handles to the superclass method failed, as did trying to use an feval statement.
EDIT: per Daniel's answer, the "glue class" (which MATLAB calls an alias class) described in the second proposal above appears to be MATLAB's recommended solution.


You are right that having to specify the name of the superclass method makes maintaing code, especially in the development stage, a real pain. I think the problem is that a class can be a direct subclass of two classes and making an automated system to handle this, generally rare case, is probably painful.
Sure, multiple inheritance could certainly be an issue if it were some default call_method_x_in_superclass. This could be avoided though if it were possible (/I could figure out how) to create a function handle for the method of the superclass, or alternatively if I could create a handle/enumeration/pointer/whatever for the superclass name.

Sign in to comment.

3 Answers

Answer by Daniel Shub
on 21 Jul 2011
 Accepted Answer

It sounds like you are concerned that if you hard code the superclass name and then you change the name of the superclass you will need to change lots of code in your subclass.
One option is instead of renaming your superclass, to newsuperclass. Create newsuperclass and make superclass a subclass of newsuperclass. Then move all the methods from superclass to newsuperclass. This preserves backwards compatibility and lets you change the name.
I think there is an oop example for this. I will check later.


Thanks for the response.
For clarity, let's say I indeed choose to change the superclass later, so that subclass < superclass < newsuperclass, and moved all methods to newsuperclass. In that case, superclass would be pretty similar (identical?) to the glue_class I mentioned above, correct?
If so, I'm starting to think that this may be the best way around the problem. I think I would want to create the glue_class directly, so that it can be guaranteed a reasonable name (e.g. classA_superclass), at least in classes whose superclasses have a decent chance to being changed. Hopping through a dummy constructor doesn't appear to take an unreasonable amount of time.
But I'll wait for the oop example if you can find it.
Ok. Thanks for the oop example. That "alias" class is exactly the glue class from my original post (assuming the class requires input arguments). It looks like this is how the mathworks wants us to handle this situation.
I guess I will accept the answer for now and edit the edit the main post. It still seems like a kludge to me, but I guess it's the best I can do.

Sign in to comment.

Answer by Andrew Newell
on 20 Jul 2011

I can't think of a good way to use dynamic superclass names, and it sounds like bad programming practice to me.
Here is a different approach, which I'll describe using the names you provided. In your superclass, define demo_method so it does nothing but create intermediate_variables and feed them to another method ( e.g., demo_method_post ) that does all the work. You could make demo_method_post hidden. Any intermediate class could overload demo_method or demo_method_post, as convenient, and in turn your subclass can either overload them or use them as is. That gives you a lot of flexibility.


The solution you mentioned lacks generality, for instance, if I would like to chain several inherited classes together. It also goes against the principle of being able to call a superclass's method from a subclass's implementation of the same method.
On a related note, I wholeheartedly disagree that this represents bad programming practice, and your response makes me think that I have not been sufficiently clear. The superclass is not dynamic in any sense: it doesn't change after the subclass is instanced and couldn't even be changed without clearing classes.
In that way, I am asking to do something that is already built into matlab: to call the immediate superclass's implementation of a method. I simply don't want to have to have the name of the superclass explicitly written in my m-code.
MATLAB also already allows one to find the name of the immediate superclass by taking the first entry in superclasses(instance_of_subclass).
I assumed I could easily combine this information into a str2func and/or feval, for instance, as:
sc = superclasses(instance_of_subclass_of_interest);
feval(str2func(['method_name@' sc{1}]), arg1,arg2,etc);
However, my attempts at this failed, and the only implementation I've tried that works without creating a glue class involves an eval statement.
Hm, is there an edit button? When I wrote, "I simply don't want to have to have the name of the superclass explicitly written in my m-code," I meant that I want to only specify the superclass in code in the first line of the classdef file and in the constructor. In other methods, it seems reasonable that I can just call method@superclass without specifying the name of the superclass explicitly.

Sign in to comment.

Answer by Andrew Newell
on 21 Jul 2011

How about just wrapping the call in its own method? If you'll forgive a little Monty Python whimsy, suppose your overloaded method is called snob and the class of interest is upperclass. Define a method upperclass_snob:
function obj = upperclass_snob(varargin)
obj = snob@upperclass(varargin{:});
Then all you have to change is the identity of upperclass.


I am pretty sure you can only call a superclass method from a subclass method with the identical name.
Yup, you can only call a superclass method with an identical name. For completeness, the ME identifier is:
And the message is:
'"@" Within a method, a superclass method of the same name is called by saying method@superclass.
The left operand of "@" must be the method name.'
I like the "little Monty Python whimsy" though

Sign in to comment.