I need to know if there is a way in matlab to define interface superclass for both handle and non-handle subclasses? If in hierarchy there is a handle class, all classes must be handle too and in matlab the interface is defined as abstract class. So it can't be done this way.
class A < handle
....
end
class AA < A
....
methods
function foo
end
end
end
class B
....
methods
function foo
end
end
I want to create some kind of container with classes AA (handle) and B (non-handle) to ensure they both have function foo. Is there way to do this?
You should be able to do:
classdef (Abstract, HandleCompatible) A
methods (Abstract)
function foo
end
end
classdef AA < A & handle
methods
function foo
...
end
end
end
classdef B < A
methods
function foo
...
end
end
end
Here you are marking A
as Abstract
, with an abstract method foo
. So AA
and B
must both implement foo
. You also mark A
as HandleCompatible
, which means that subclasses can be handles.
Then AA
inherits from both A
and handle
, but B
inherits only from A
and is a value class. Both implement foo
.
Note that you may need to take care in the way that you implement foo in each class, as the necessary function signature can vary between handle and value classes.