Accessing subclass methods from abstract superclass
Show older comments
Hi all,
Longtime listener, first time caller. I'm attempting to augment an abstract superclass with a method that calls (concrete) methods from a subclass. In the example below, I would like to call ChildClass.doC() and see, in the Command Window:
Child class, doA()
Child class, doB()
Here is my MWE.
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC()
doA();
doB();
end
end
end
classdef ChildClass < ParentClass
%CHILDCLASS Testing properties of abstract classes
methods (Static = true)
function doA()
disp('Child class, doA()')
end
function doB()
disp('Child class, doB()')
end
end
end
Upon the call to ChildClass.doC(), what I would like is for MATLAB to call ParentClass.doC(), realize that doA() and doB() are abstract methods, and go back `up' the call chain to ChildClass.doA(), ChildClass.doB(). I've been told this is a construct in C++, not sure if its possible here.
I'm doing this because I have many classes that inherit from ParentClass, and each implement their own version of doA(), doB(). These functions are typically used together, and I would like to make a `shorthand' a. la doC() in the parent class without having to write a seperate doC() method for each subclass. Thanks.
Accepted Answer
More Answers (3)
per isakson
on 24 Dec 2020
This is not what you ask for, but close. (I don't think what you want is possible with Matlab.)
>> child = ChildClass();
>> child.doC
Child class, doA()
Child class, doB()
where
classdef ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods % (Static = true)
function doC(this)
this.doA();
this.doB();
end
end
end
and
classdef ChildClass < ParentClass
%CHILDCLASS Testing properties of abstract classes
methods (Static = true)
function doA(~)
disp('Child class, doA()')
end
function doB(~)
disp('Child class, doB()')
end
end
end
Catalytic
on 25 Dec 2020
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC()
ChildClass.doA();
ChildClass.doB();
end
end
end
Probably not exactly what you were hoping for, but this solution does make sense when doC@ParentClass contains a lot of code. Note that doC@ChildClass is very short and can be simply copy/pasted as is to any other child class that you make.
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC(name)
doA=str2func(name+".doA");
doA=str2func(name+".doB");
doA();
doB();
end
end
end
classdef ChildClass < ParentClass
%CHILDCLASS Testing properties of abstract classes
methods (Static = true)
function doA()
disp('Child class, doA()')
end
function doB()
disp('Child class, doB()')
end
function doC()
doC@ParentClass(mfilename)
end
end
end
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!