Setting a Base Class property from Derived class

13 views (last 30 days)
Hello all..I am a begineer to OOPS in MATLAB . I need to set the property in baseclass from the derived class. How should I do that.. This is my example
DERIVED CLASS
classdef classA < classB
properties
A=2;
b=3;
end
properties
mystring='MATLAB';
end
end
BASE CLASS
classdef classB < classC
properties
myValue=[]
end
methods
%%REST OF THE CODE %%%
end
end
I would like to set the the property myValue (which is in Baseclass) from the derived class. Can anyone help me out in this... Apologies if it's a newbie questions..
NOTE : I dont want to define that property in the derived class and get its value in base class. The reason is : I have lot of derived classes where I might need to set the property myVal (or) in some cases dont need to set it. So i need to be empty for my code in BASE CLASS
  9 Comments
Aryan Sinha
Aryan Sinha on 6 Apr 2016
This is just an example of what I am trying to do.. My code has lot of stuff in it with UnitTesting in it...
My question in a straight way was to overriding the super class properties in subclass using get method,,,
Adam
Adam on 6 Apr 2016
Ok, what you say there does hint at a problem, but does not seem to relate to the code example you showed using a home-made function called setObj. I will add an answer below relevant to your point in bold which may or may not be of use, depending exactly what your problem is.

Sign in to comment.

Answers (2)

per isakson
per isakson on 5 Apr 2016
Edited: per isakson on 5 Apr 2016
I tried this, which works fine. What do you try to do?
>> ca = ClassA
ca =
ClassA with properties:
A: 2
b: 3
mystring: 'MATLAB'
myValue: []
>> ca.setobj
>> ca
ca =
ClassA with properties:
A: 2
b: 3
mystring: 'MATLAB'
myValue: 1234
>>
where
classdef ClassA < ClassB
properties
A=2;
b=3;
end
properties
mystring='MATLAB';
end
methods
function setobj(obj)
obj.myValue=1234;
end
end
end
and
classdef ClassB < handle
properties
myValue=[]
end
methods
%%REST OF THE CODE %%%
end
end
One more trial. No problems, works according to documentation
>> ca = ClassA
ca =
ClassA with properties:
A: 2
b: 3
mystring: 'MATLAB'
myValue: []
>> ca.myValue = 4321
ca =
ClassA with properties:
A: 2
b: 3
mystring: 'MATLAB'
myValue: 4321
  1 Comment
Steven Lord
Steven Lord on 5 Apr 2016
This works because ClassB is a handle class. If it were not, your ClassA setobj method would need to return the modified object and you'd need to call it with an output argument.

Sign in to comment.


Adam
Adam on 6 Apr 2016
Edited: Adam on 6 Apr 2016
Taking a bit of a guess from your comments above, this may be relevant to your problem, but either way it is hopefully useful information anyway.
When you have a property in a base class you are free to get and set this in the derived class (provided it has at least protected access. You would do this as you would anywhere simply by e.g.
myClass.myValue = 1234;
This will call get.myValue in the base class because it is a base class property. Everything here is fine.
What you cannot do is try to overide the get.myValue function in your derived class. myValue is a property of the base class and only the base class can define its get function. So you cannot overide this with different behaviour in different derived classes.
What you can do and I have done often in my work is a construct like the following:
classdef myBaseClass < handle
properties( Dependent, Access = public )
myValue = [];
end
methods
function set.myValue( obj, value )
doSetMyValue( obj, value );
end
function value = get.myValue( obj )
value = doGetMyValue( obj );
end
end
methods( Access = protected )
doSetMyValue( obj, value )
value = doGetMyValue( obj )
end
end
classdef myDerivedClass < myBaseClass
properties( Access = private )
myValue_ = [];
end
methods( Access = protected )
function doSetMyValue( obj, value )
obj.myValue_ = value + 7;
end
function value = doGetMyValue( obj )
value = obj.myValue_ + 7;
end
end
end
classdef myDerivedClass2 < myBaseClass
properties( Access = private )
myValue_ = [];
end
methods( Access = protected )
function doSetMyValue( obj, value )
obj.myValue_ = value * 2;
end
function value = doGetMyValue( obj )
value = obj.myValue_ * 2;
end
end
end
This is a completely contrived example that does something ridiculous, but shows the derived classes each defining the behaviour of getting and setting the base class property whilst still having the property defined in the base class and therefore available to all classes.
In this case the base class forces the derived classes to define this behaviour. You could also setup default behaviour in the base class so that derived classes only override it if they want to. Be careful doing this though, you need to store a second noon-dependent property. My first attempt, a little rusty from when I last did this setup, led me into infinite recursion! Using the above you would get, for example:
>> d = myDerivedClass;
>> d.myValue = 6
d =
myDerivedClass with properties:
myValue: 20
>> d2 = myDerivedClass2;
>> d2.myValue = 6
d2 =
myDerivedClass2 with properties:
myValue: 24

Categories

Find more on Handle Classes 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!