MATLAB: In MATLAB OOP, is it possible for a parent method to determine whether a child descendant overrides a certain other method of the parent

inheritancemethodsoopoverride

To make that question a bit more concrete:
Suppose a parent class has methods "foo1" and "foo2".
Various child classes are descended from this parent class. Some of the children override foo1 whereas other children do not override it.
The question is, when the inherited method foo2 is called by a child, is there any way for foo2 to determine whether its calling child has overridden foo1?
I note that methods(obj,'-full') prints the required information to the command window, but unfortunately cellar=methods(obj,'-full') does not save that information in cellar. (Anyway, it seems pretty clumsy to be handling cell arrays of strings to get this information.)
Thanks for any suggestions.

Best Answer

You probably shouldn't assume that the method in which you're interested is method 30. Instead use findobj to find the entry in the MethodList with 'Name' equal to 'foo1'. Here is the class I will use for this example:
>> dbtype testForPlus.m
1 classdef testForPlus < matlab.unittest.TestCase
2 methods(Test)
3 function onePlusOne(testcase)
4 testcase.verifyEqual(1+1, 2);
5 end
6 end
7 end
Let's find the verifyEqual method of this class. It is defined in the matlab.unittest.qualifications.Verifiable superclass of matlab.unittest.TestCase.
>> theMetaclass = ?testForPlus;
>> methodList = theMetaclass.MethodList;
>> VE = findobj(methodList, 'Name', 'verifyEqual');
>> definer = VE.DefiningClass.Name
definer =
matlab.unittest.qualifications.Verifiable
Now let's try to find a method that doesn't exist in the class.
>> doesNotExist = findobj(methodList, 'Name', 'doesNotExist')
doesNotExist =
0×0 method array with properties:
*snip list of properties*
Use isempty on the doesNotExist object to check if the method for which you searched does not exist.