Let's say I have a class "A" that looks like this:
classdef A properties (GetAccess = public, SetAccess = protected) foo; end methods function f = get.foo(self) %#ok<MANU>
f = randn(1, 1); end endend
And now let's say I want to override the "foo" property in a child class that returns a uniformly random variable. In just about every other object-oriented programming language with which I am familiar, I would do the following:
classdef B < A properties (GetAccess = public, SetAccess = protected) foo; end methods function f = get.foo(self) %#ok<MANU> f = rand(1, 1); end endend
But in MATLAB, this is not the right answer. I try this and here's the error that I see:
>> b = B Error using BCannot define property 'foo' in class 'B' because the property has already been defined in the super-class 'A'.
I have tried adding the Dependent tag to the property to no avail, also, Abstract in this case will also not work because (according to the documentation), the properties in the "concrete" classes must be static (i.e., not dynamically calculated).
This is really hindering my work to create a clean design in how some of my tools work together. Can somebody please provide some insight into how to accomplish this?
Thanks!
Best Answer