Borrar filtros
Borrar filtros

Force super class to not call overloaded methods of the subclass

30 visualizaciones (últimos 30 días)
Naum Derzhi
Naum Derzhi el 5 de Mayo de 2021
Editada: Matt J el 28 de Abr. de 2024 a las 21:34
I have a base class and subclass inheriting from it. Base class has method A. Subclass overloads that method. I want to force the base class to use (only in some places) it's own method, not the one overloaded by subclass. Is there any way to do it?
Thank you

Respuestas (3)

Matt J
Matt J el 28 de Abr. de 2024 a las 21:10
Editada: Matt J el 28 de Abr. de 2024 a las 21:21
In any superclass method, you can test whether the invoking object is of the base class or one of its children. Depending on the result of this test, you can clone the child's base component for the purposes of calling base methods only. The attachments give an example of this.
parent=myclass(1), child=mysubclass(1,2)
parent =
myclass with properties: a: 1
child =
mysubclass with properties: b: 2 a: 1
parent.callingMethod()
Parent method called
child.callingMethod()
Child Method called
child.callingMethod(scope="Parent")
Parent method called
  1 comentario
Matt J
Matt J el 28 de Abr. de 2024 a las 21:33
Editada: Matt J el 28 de Abr. de 2024 a las 21:34
You can also have the desired calling scope set through the a property, as in the attached modifications. This would be the better design if you want all methods to make this sort of scope check.
parent=myclass(1), child=mysubclass(1,2)
parent =
myclass with properties: a: 1 callingScope: "all"
child =
mysubclass with properties: b: 2 a: 1 callingScope: "all"
parent.callingMethod()
Parent method called
child.callingMethod()
Child Method called
child.callingScope="Parent";
child.callingMethod()
Parent method called

Iniciar sesión para comentar.


Shraddha Jain
Shraddha Jain el 22 de Jun. de 2021
Hi Naum,
When you create an object obj of a superclass and then use obj to call the overloaded (or any) method of the superclass, the method defined in the superclass is called and not the one of the subclass.
Refer to this documentation for more information.
  2 comentarios
Naum Derzhi
Naum Derzhi el 25 de Jun. de 2021
Hello Shraddha,
Sorry, but this does not help. What I want to happen is this: when super class has funcitons F1 and F2, and F2 calls F1, I want that F2 would call F1 of the SuperClass, not of the SubClass - in certain circumstances, when I tell it so.
Please, see the attached example classes. Both in SubClass and SuperClass the method F2 calls the method F1. In the subclass these methods first call the corresponding method of the SuperClass, which is the usual practice.
And this is what happens: when SubClass.F2 calls the SuperClass F2, the latter calls F1 of the SubClass. I want it to call F1 of the SuperClass. Maybe not always, but only when I want exactly this behavior. This is simple in C++. How can it be done in Matlab?
Thanks,
Naum
A.B.
A.B. el 26 de Abr. de 2024 a las 4:26
Editada: A.B. el 26 de Abr. de 2024 a las 4:27
I am facing the same issue in my code and I have been wasting a few weeks now to find out how I can prevent the SuperClass to call Subclass overloaded method. This is an absolutely silly OOP behavior in MATLAB. Why isn't there an easy way to do so?

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 26 de Abr. de 2024 a las 4:55
You can call a superclass method even if the subclass overloads or overrides that method only from within that method itself. So if the superclass and subclass both define a method foo(), inside the subclass foo() method you can call the superclass's foo() method.
You can also have the subclass's constructor call the superclass's constructor (even though they don't have the same name.)
See this documentation page for instructions on how to do so.
  3 comentarios
Steven Lord
Steven Lord el 26 de Abr. de 2024 a las 12:08
I'm not sure I understand what you're trying to do. Can you describe in a little more detail what problem you're trying to solve (not how you're hoping to implement it; describe the why not the how), or perhaps what design pattern you're trying to implement? With that information we may be able to suggest an approach to achieve your goal (or explain if it smells a bit funny to us.)
A.B.
A.B. el 28 de Abr. de 2024 a las 19:41
Editada: A.B. el 28 de Abr. de 2024 a las 19:45
The superclass has method make and premake. The subclass also overloads both methods. It appears any method within the superclass which is calling make or premake, calls the overloaded methods of the subclass (and not those of superclass). This behavior messes up everything. I sincerely hope I am doing something wrong, otherwise this behavior is completely counter intuitivie for me.

Iniciar sesión para comentar.

Categorías

Más información sobre Subclass Definition en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by