Is there any workaround to call a superclass method in a subclass method with a different name?
Solution 1:
If you want to be able to call method1@sup
with an object of class sub
, you shouldn’t override the method1
function. If you override a function, you are saying that the base classes’ function is not suitable for this class and needs to work differently.
You might want to just write a function with a different name.
One way to do so is to put the implementation of sup.method1
in a protected function, which you can then call from both sup.method1
and sub.method2
:
classdef sup
methods
function method1(obj,val)
method1_impl(obj,val);
end
end
methods(Access=protected)
function method1_impl(obj,val)
fprintf('sup val=%g\n',val);
end
end
end
classdef sub < sup
methods
function method1(obj,val)
fprintf('sub val=%g\n',val);
end
function method2(obj,val)
method1_impl(obj,val);
end
end
end