Calling a superclass method from any method

19 views (last 30 days)
Zangdaarr
Zangdaarr on 14 Jul 2017
Edited: Zangdaarr on 19 Jul 2017
Hi,
I have a base class:
classdef foo < handle
function methodA( self )
end
And a base class:
classdef goo < foo
function methodA( self )
if ( condition )
self.methodA@foo();
else
other stuff
end
end
function methodB( self )
self.methodA@foo();
end
end
Why isn't this possible ?
Every other langage allows it, I do not get the point. Is there any way to get it to work ?
I need A to be the public interface: the framework user is not allowed to bypass the override. However B is framework private, needed for internal management of values. So no, I cannot invert A & B in the child class nor I cannot rename A.

Answers (2)

Robert U
Robert U on 19 Jul 2017
Edited: Robert U on 19 Jul 2017
Hi Zangdaar,
I am not sure if I got your point correctly. In my understanding you have two base classes where class_B has to invoke a method of class_A. Since I do not know what data you would like to share between the objects I introduce a random property.
Base class A:
classdef class_A < handle;
properties
end
methods
function obj = methodA(obj)
disp('<< Method A of class_A utilized!');
end
end
end
Base class B:
classdef class_B < handle;
properties
classA_Property
end
methods
function obj = methodA(obj)
disp('<< Method A of class_B utilized!');
c = clock;
disp(['<< actual seconds: ',num2str(fix(c(6)))])
if mod(fix(c(6)),2) == 0
TmpClassA = class_A;
TmpClassA.methodA;
obj.classA_Property = TmpClassA;
else
disp('<< methodA in class_B continued.');
end
end
function obj = methodB(obj)
disp('<< Method B of class_B utilized!');
obj.methodA;
end
end
end
In that case you create a temporary object of class_A on which you can utilize method_A of class_A that can have the same name as method_A in class_B.
Second option in my opinion is, to use class_A as superclass for class_B_inh_A. In that case it is possible to call the methodA of class_A that is shadowed by methodA of class_B_inh_A. The only thing is that it is not possible to call it from methodB which brings us back to the temporary class_A call:
classdef class_B_inh_A < class_A;
properties
classA_Property;
end
methods
function obj = methodA(obj)
disp('<< Method A of class_B_inh_A utilized!');
c = clock;
disp(['<< actual seconds: ',num2str(fix(c(6)))])
if mod(fix(c(6)),2) == 0;
obj.methodA@class_A;
else
disp('<< methodA in class_B_inh_A continued.');
end
end
function obj = methodB(obj)
disp('<< Method B of class_B_inh_A utilized!');
obj.methodA;
TmpClassA = class_A;
TmpClassA.methodA;
obj.classA_Property = TmpClassA;
end
end
end
Calling class_A method A:
>> A = class_A;
>> A.methodA;
<< Method A of class_A utilized!
Calling class B method A:
>> B = class_B;
>> B.methodA;
<< Method A of class_B utilized!
<< actual seconds: 45
<< methodA in class_B continued.
>> B.methodA;
<< Method A of class_B utilized!
<< actual seconds: 52
<< Method A of class_A utilized!
Calling class B method B:
>> B.methodB;
<< Method B of class_B utilized!
<< Method A of class_B utilized!
<< actual seconds: 24
<< Method A of class_A utilized!
<< Method A of class_A utilized!
>> B.methodB;
<< Method B of class_B utilized!
<< Method A of class_B utilized!
<< actual seconds: 25
<< methodA in class_B continued.
<< Method A of class_A utilized!
Calling class B_inh_A method A:
>> B_inh_A = class_B_inh_A;
>> B_inh_A.methodA;
<< Method A of class_B_inh_A utilized!
<< actual seconds: 45
<< methodA in class_B_inh_A continued.
>> B_inh_A.methodA;
<< Method A of class_B_inh_A utilized!
<< actual seconds: 46
<< Method A of class_A utilized!
Calling class B_inh_A method B:
>> B_inh_A.methodB;
<< Method B of class_B_inh_A utilized!
<< Method A of class_B_inh_A utilized!
<< actual seconds: 53
<< methodA in class_B_inh_A continued.
<< Method A of class_A utilized!
>> B_inh_A.methodB;
<< Method B of class_B_inh_A utilized!
<< Method A of class_B_inh_A utilized!
<< actual seconds: 54
<< Method A of class_A utilized!
<< Method A of class_A utilized!

Zangdaarr
Zangdaarr on 19 Jul 2017
Edited: Zangdaarr on 19 Jul 2017
Hey I'm really sorry, I made a typo in my example, so, Goo inherit Foo. Otherwise the question is pointless :/
Thanks a lot however for the long and detailed answer.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!