Calling subclass functions to manipulate inputs for superclass constructor?

2 views (last 30 days)
I have a user-created subclass Domain2d < fegeometry. The fegeometry class is a default class in MATLAB, and the inputs for the fegeometry constructor are not user-friendly. Part of the idea of the Domain2d subclass is that its constructor should take user-friendly inputs, translate them into the complex inputs needed for the fegeometry constructor, then call the fegeometry constructor. The way the code is currently written, the task of manipulating the simple inputs into complex inputs is like:
classdef Domain2d < fegeometry
properties
end
methods
function self = Domain2d(simple_inputs)
% manipulate simple_inputs to complex_inputs
...(code here)
% call superclass constructor
self@fegeometry(complex_inputs);
end
end
end
This is kludge-y, but it works, and substantial code has been written that uses the bar object. But now I have a good reason to want to rewrite the bar object like
classdef Domain2d < fegeometry
properties
end
methods
function self = Domain2d(simple_inputs)
complex_inputs = translateSimpleToComplex(self,simple_inputs);
self@fegeometry(complex_inputs);
end
function complex_inputs = translateSimpleToComplex(self,simple_inputs)
...
end
end
end
The above produces the error "Calling the superclass constructor 'fegeometry' after an object use or after a return statement is not supported."
How should I proceed?
  2 Comments
Matt J
Matt J on 27 Jun 2024
Edited: Matt J on 27 Jun 2024
The handle class is a default class in MATLAB, and the inputs for the handle constructor are not user-friendly
Does the handle class constructor even accept inputs? What documentation are you getting this from? I've never heard of anyone explicitly calling the handle superclass constructor.
Tyler Fara
Tyler Fara on 27 Jun 2024
Edited: Tyler Fara on 27 Jun 2024
Sorry, my dummy names were maybe poorly chosen. I've seen people use the terms "handle" and "bar" to refer to a superclass and a subclass; I guess because a bar is a part of a handle? But in MATLAB, a handle is also its own class right? Anyway, what I meant by "handle" and "bar" were two dummy classes. The actual class names are Domain2d < fegeometry and I've edited my question to reflect these names.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 27 Jun 2024
I have a user-created subclass bar < handle.
Don't call your class bar. It already has a meaning in MATLAB.
Looking at the body of your constructor:
function self = bar(simple_inputs)
complex_inputs = translateSimpleToComplex(self,simple_inputs);
self@handle(complex_inputs);
end
the error message is correct. You cannot use self as an input to the translateSimpleToComplex function and then afterwards try to call the superclass constructor to create/initialize the variable self.
Why are you passing self as an input into translateSimpleToComplex? Is it simply so you can call the method and you're not actually trying to call methods or access properties of your class? If so turn it from an instance method (one that requires an instance of the class as input) into a Static method and call it using the name of your class, mybar.translateSimpleToComplex(simple_inputs) if you rename your class to mybar as an example.
  3 Comments
Tyler Fara
Tyler Fara on 27 Jun 2024
OK TL;DR, your suggestion about static methods should work. Thank you so much!! You know when you pick up a new skill or tool and you just know in advance that it's going to be super powerful? That's this moment right now for me, and that's thanks to you. I appreciate it!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!