How to write a subclass that `copies’ an instance of the superclass?
Basic Question: Can I make a copy of an instance of a class, but with a different version of one of the class’s methods?
Details: I have a class called Domain2d which has a method threePointQuadrature. I want to rewrite the threePointQuadrature method and run tests to compare the new method versus the old method. The idea I have is to make Domain2d_exp < Domain2d which is the same in every way as Domain2d, except I will write an experimental version of the threePointQuadrature method in Domain2d_exp.
My understanding is that subclasses inherit all methods from the superclass, but will run the subclass version of a method if the subclass has a method with the same name as one of the superclass methods. So, if I have dom (an instance of Domain2d) and dom_exp (an instance of Domain2d_exp) then when I call dom.threePointQuadrature, the original version of the function will be run, and when I call dom_exp.threePointQuadrature, the the experimental version of threePointQuadrature will be run.
Now, since Domain2d_exp < Domain2d I would normally write the constructor for Domain2d_exp to take the same parameters as Domain2d, and then just call the superclass constructor for Domain2d from within the constructor for Domain2d_exp. However, the issue in this case is that once a Domain2d object has been instantiated, it must be configured, which requires several other lines of code, and is somewhat costly in terms of time. So what I’d rather do is instantiate and configure dom, and instance of Domain2d, and then pass dom to the constructor for Domain2d_exp, which will retain all the properties and methods of dom, but with the experimental threePointQuadrature method.
Is this possible? Or, is there a better way to acheive the same end result?Basic Question: Can I make a copy of an instance of a class, but with a different version of one of the class’s methods?
Details: I have a class called Domain2d which has a method threePointQuadrature. I want to rewrite the threePointQuadrature method and run tests to compare the new method versus the old method. The idea I have is to make Domain2d_exp < Domain2d which is the same in every way as Domain2d, except I will write an experimental version of the threePointQuadrature method in Domain2d_exp.
My understanding is that subclasses inherit all methods from the superclass, but will run the subclass version of a method if the subclass has a method with the same name as one of the superclass methods. So, if I have dom (an instance of Domain2d) and dom_exp (an instance of Domain2d_exp) then when I call dom.threePointQuadrature, the original version of the function will be run, and when I call dom_exp.threePointQuadrature, the the experimental version of threePointQuadrature will be run.
Now, since Domain2d_exp < Domain2d I would normally write the constructor for Domain2d_exp to take the same parameters as Domain2d, and then just call the superclass constructor for Domain2d from within the constructor for Domain2d_exp. However, the issue in this case is that once a Domain2d object has been instantiated, it must be configured, which requires several other lines of code, and is somewhat costly in terms of time. So what I’d rather do is instantiate and configure dom, and instance of Domain2d, and then pass dom to the constructor for Domain2d_exp, which will retain all the properties and methods of dom, but with the experimental threePointQuadrature method.
Is this possible? Or, is there a better way to acheive the same end result? Basic Question: Can I make a copy of an instance of a class, but with a different version of one of the class’s methods?
Details: I have a class called Domain2d which has a method threePointQuadrature. I want to rewrite the threePointQuadrature method and run tests to compare the new method versus the old method. The idea I have is to make Domain2d_exp < Domain2d which is the same in every way as Domain2d, except I will write an experimental version of the threePointQuadrature method in Domain2d_exp.
My understanding is that subclasses inherit all methods from the superclass, but will run the subclass version of a method if the subclass has a method with the same name as one of the superclass methods. So, if I have dom (an instance of Domain2d) and dom_exp (an instance of Domain2d_exp) then when I call dom.threePointQuadrature, the original version of the function will be run, and when I call dom_exp.threePointQuadrature, the the experimental version of threePointQuadrature will be run.
Now, since Domain2d_exp < Domain2d I would normally write the constructor for Domain2d_exp to take the same parameters as Domain2d, and then just call the superclass constructor for Domain2d from within the constructor for Domain2d_exp. However, the issue in this case is that once a Domain2d object has been instantiated, it must be configured, which requires several other lines of code, and is somewhat costly in terms of time. So what I’d rather do is instantiate and configure dom, and instance of Domain2d, and then pass dom to the constructor for Domain2d_exp, which will retain all the properties and methods of dom, but with the experimental threePointQuadrature method.
Is this possible? Or, is there a better way to acheive the same end result? classes, constructors MATLAB Answers — New Questions