title: MATLAB OOP: Communication between objects of different subclasses
body: <p>I am at the beginning of a bigger project, where I have to rewrite existing MATLAB script code. I was asked to use MATLAB's object oriented programming support to get a more flexible and robust program.
  I got to a point, where I wondered how to let objects of different subclasses communicate or better: what is the best or most elegant/efficient/user friendly way to do that.  </p>
  
  <p>Example:<br>
  Superclass A (handle class):</p>
  
  <pre><code>classdef A &lt; handle
    properties
      myvar
    end
  
    methods (Access = protected)
      function calc_myvar(obj)
        %calculate myvar with some code
        obj.myvar=...;
      end
    end
  end
  </code></pre>
  
  <p>Subclass B:</p>
  
  <pre><code>classdef B &lt; A
    properties
      subclassvar
    end
  
    methods (Access = protected)
      function calc_subclassvar(obj)
        %calculate subclassvar with some code
        %needs myvar of an object of class C
        %C.myvar
        obj.subclassvar=...;
      end
    end
  end
  </code></pre>
  
  <p>Subclass C:</p>
  
  <pre><code>classdef C &lt; A
    properties
      %some other properties
    end
  
    methods
      %some other methods
    end
  end
  </code></pre>
  
  <p>So subclass B needs a variable of subclass C that is defined in A. At the moment I always pass an object of C as additional input parameter to the function. Additionally I don't know if C.myvar already has a value.<br>
  Current implementation:</p>
  
  <pre><code>function calc_subclassvar(obj,C)
    if isempty(C.myvar)
      C.calc_myvar;
    end
    obj.subclassvar = do_something_with_C.myvar;
  end
  </code></pre>
  
  <p>Is there another, better way? I read of overloading the <code>get</code> function, so I don't have to check everytime if the variable exists? And I read about events and listeners but couldn't get it to work satisfactorily. For example if I want to add a listener to C it has to know from which specific object of A the event is sent. Or is there a way that C just listens for any object of A?<br>
  Maybe you know another way. It's kind of confusing. =)</p>