Access property of "superior" class (not superclass)

5 views (last 30 days)
Hi all, I just wonder how to access a property of some "higher" class (object) from a "lower" class (object). Do not know how to explain clearly, so here is an example:
A = Class_higher();
B = Class_lower();
Imagine, there is a property prop_A in A, so you can access it e.g.:
A.prop_A;
Then I store B into A.prop_A:
A.prop_A = B;
Finally imagine, there is some method meth_B in B, so you can call it:
A.B.meth_B;
My question is: Is it possible to access prop_A through meth_B?
Note A is not the superclass of B...
Thanks, Adam

Accepted Answer

Guillaume
Guillaume on 26 Feb 2015
Edited: Guillaume on 26 Feb 2015
There is not really a term to describe your relationship between A and B other than to say that A has a member variable that is an instance of B.
It is of course not possible to access any private properties or methods of A from B. That is the whole tenet of encapsulation in OOP. Only the class (or derived classes) can access private members.
B can of course access any public member of A, and it can also access any member for which you give it explictly access (see the doc).
Now for an instance of B to access a member (public or one it's been granted access to) of the A instance that holds it, it needs itself to hold a reference to that A (hence A have to be a handle class). Therefore you would need:
classdef A < handle
properties (SetAccess = private, GetAccess = ?B) %B (and A of course) can read it
prop1 = 7; %property to be read by B
end
properties (SetAccess = private)
propB; %hold B instances
end
methods
function this = A() %constructor
this.propB = B(this); %pass a reference to self to B constructor
end
end
end
classdef B
properties (Access = private)
owner; an instance of A
end
methods (Access = ?A) %only A can construct B?
function this = B(Ainstance) %constructor
this.owner = Ainstance;
end
end
methods
function delete(this) %destructor. not sure it's needed.
this.owner = [];
end
function method_that_uses_A(this)
disp(this.owner.prop1)
end
end
end
Then:
a = A;
a.propB.method_that_usesA
displays
7
  2 Comments
TADA
TADA on 6 Nov 2018
Edited: TADA on 6 Nov 2018
You can set A as a member of B when in the property access method
classdef A < handle
properties
prop1 = 7; % property to be read by B
end
properties
child; % hold B instances
end
methods % property accessors
function val = set.child(this, value)
this.child = value;
% validate duck type
if ~isprop(value, 'owner')
error('a child of A must have a property owner');
end
this.child.owner = this;
end
end
end
classdef B
properties
owner; % an instance of A
end
methods % property accessors
function set.owner(this, value)
% validate duck type
if ~isprop(value, 'prop1')
error('class B owner must expose a property named prop1');
end
this.owner = value;
end
end
methods
function delete(this) %destructor. not sure it's needed.
this.owner = [];
end
function method_that_uses_A(this)
disp(this.owner.prop1)
end
end
end
now A and B are less strongly coupled, and the whole solution relies on duck typing, the benefits of scripting languages.
for instance you can set an instance of class C in A.child as long as it has an owner property, or you can put C as the owner of B as long as it has a property prop1

Sign in to comment.

More Answers (1)

Matt J
Matt J on 26 Feb 2015
Edited: Matt J on 26 Feb 2015
Finally imagine, there is some method meth_B in B, so you can call it: A.B.meth_B;
I think you really mean
A.prop_A.meth_B(arg1,arg2,...)
Typing A.B.meth_B would generate an error, since 'B' is not the name of a property of A.
My question is: Is it possible to access prop_A through meth_B?
Since prop_A contains an instance of class B, that instance will get passed to meth_B when you type the above. You should have written meth_B in the form
function meth_B(objB, arg1, arg2)
...
end
The first argument, objB passed into the workspace of meth_B will always be the calling object, in this case whatever class B instance that you previously put in A.prop_A.
All of this is true regardless of the relationship superior/inferior/subclass/superclass between A and B. If your real question was whether it is possible to access the entire object A from inside meth_B, the answer is no.
  1 Comment
Adam Koutný
Adam Koutný on 27 Feb 2015
Yes, you right, I meant that call you wrote, it was my mistake. Thank you for the answer!

Sign in to comment.

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!