Force super class to not call overloaded methods of the subclass
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (2)
Shraddha Jain
il 22 Giu 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.
2 Commenti
A.B.
circa 17 ore fa
Modificato: A.B.
circa 17 ore fa
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?
Steven Lord
circa 17 ore fa
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.)
2 Commenti
A.B.
circa 16 ore fa
Spostato: Steven Lord
circa 10 ore fa
Thank you, I appreciate your response, although that is still not quite helpful. I can bypass the whole problem if there was a way to set the superclass components of the subclass in an assignment. For example,
```matlab
self@superclass = superObject
```
where superObject is an object of superclass. I know some other languages allow such a syntax. Is there a MATLAB equivalent for this functionality?
Steven Lord
circa 10 ore fa
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.)
Vedere anche
Categorie
Scopri di più su Battery Pack Modeling in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!