Calling subclass functions to manipulate inputs for superclass constructor?

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

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.
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

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

I apologize, I was trying to use dummy variable names and I have seen handle and bar used elsewhere to refer to a dummy super class and sub class. The actual class names are Domain2d < fegeometry, and I have edited my original question to use these.
The reason I am passing self as an input into translateSimpleToComplex is the following:
Domain2d represents a rectangular spatial domain Omega = (x_1,x_2) x (y_1,y_2) with a triangular mesh whose triangles have max edge length h, and is called like dom = Domain2d(xBounds,yBounds,h). The constructor for Domain2d takes these inputs and creates a decomposed geometry matrix which is then given to the superclass constructor for the fegeometry class.
But now, I want to created a new subclass PuncturedDomain2d < Domain2d where PuncturedDomain2d represents a rectangular domain with one or more "holes" cut out of it. This domain with the holes cut out can also be described using a decomposed geometry description matrix. But I don't want the user to have to specify the decomposed geometry description matrix; rather I want them only to have to describe the xBounds, yBounds, h, and then the desired number of holes.
So, both Domain2d and PuncturedDomain2d need to create a decomposed geometry description matrix in order to call the fegeometry constructor. But the method for creating the decomposed geometry description matrix is different for the Domain2d object than for the PuncturedDomain2d object.
Now, I know that if a superclass constructor uses a member function, and then the superclass constructor is called from the subclass, and the subclass has its own version of that member function, then the superclass constructor will actually use the member function from the subclass. So what I was hoping to do is to write a different version of the translateSimpleToComplex function within Domain2d and PuncturedDomain2d. And that way, when PuncturedDomain2d calls the constructor for Domain2d, the decomposed geometry description matrix will actually be created using the translateSimpleToComlex function in PuncturedDomain2d.
Not sure if any of that makes sense or is useful. The answer you've already provided might work, so I will look into that. But hopefully all the gory details are somehow useful for informing your answer!
Thanks!
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)

Categories

Community Treasure Hunt

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

Start Hunting!