MATLAB Answers

Call superclass method without knowing name of superclass?

13 views (last 30 days)
Jared
Jared on 20 Jul 2011
Edited: 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:
function_name@superclass_of_interest(arg1,arg2,etc)
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
methods
function obj = glue_class(varargin)
obj = obj@superclass_of_interest(varargin{:});
end
end
end
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.
  5 Comments
Jared
Jared on 21 Jul 2011
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.

Accepted Answer

Daniel Shub
Daniel Shub on 21 Jul 2011
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.
  2 Comments
Jared
Jared on 21 Jul 2011
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.

More Answers (2)

Andrew Newell
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.
  2 Comments
Jared
Jared on 20 Jul 2011
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.


Andrew Newell
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{:});
end
Then all you have to change is the identity of upperclass.
  3 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!