Call superclass method without knowing name of superclass?
5 views (last 30 days)
Show older comments
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
Daniel Shub
on 21 Jul 2011
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.
Accepted Answer
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.
More Answers (2)
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
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
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!