Overwriting a setter or getter in a subclass: why is this not possible???
31 views (last 30 days)
Show older comments
This is a real essential thing to be able to do in a project I'm working on at the moment, but it appears this is not allowed in MATLAB for some strange reason...
Is there a reason for this I cannot think of, or is it just a limitation of OOP in MATLAB?
classdef SubClass < SuperClass
methods
function obj = set.SomePropDefinedInTheSuper(obj,newVal)
end
end
end
3 Comments
John
on 22 May 2012
It is mentioned in the Matlab documentation that modifying setters and getters in a subclass are not possible. I also do not understand why. It would be a useful feature in a future release.
From the documentation:<http://www.mathworks.nl/help/techdoc/matlab_oop/brenyev-1.html#brenyev-6>
Modifying Superclass Properties
There are two separate conditions under which you can redefine superclass properties:
The value of the superclass property Abstract attribute is true
The values of the superclass property SetAccess and GetAccess attributes are private
In the first case, the superclass is just requesting that you define a concrete version of this property to ensure a consistent interface. In the second case, only the superclass can access the private property, so the subclass is free to reimplement it in any way.
Answers (2)
Daniel Shub
on 22 May 2012
I don't know much about OOP concepts or implementations in any language (including MATLAB), but I think in general there is confusion about the differences between properties and methods. From my understanding languages that support OOP, including MATLAB, make it trivial to overload methods. Overloading properties tends to be a pain. I think in Python you can, but it is a real pain. I am not sure if you can in C or C#. In MATLAB you cannot do it, but you can work around it in at least two ways.
The first thing you should ask yourself is do you want to overload the property or the method? This is in fact the first work around. Have the superclass have a property and a set method for the property. This is not the set.myProp function, but rather a method you name and define (possibly setMyProp). The setMyProp method would then set the myProp property. This would allow a subclass to overload the setMyProp method.
You could create a new property mySubProp in the subclass and overload subsref to intercept calls to obj.myProp and route them to obj.mySubProp. This can be a pain given how subsref works within methods of the class.
0 Comments
Damien Watson
on 17 Oct 2023
For anyone coming across this thread now, this can be done by editting the superclass to allow it eg:
classdef SuperClass
properties
MyProp
end
methods
function value = get.MyProp(obj)
% Getter simply calls overridable function
value = obj.GetMyProp;
end
function value = GetMyProp(obj)
value = obj.MyProp;
end
end
end
Then, in the subclass, simply override the method that you made the getter call.
classdef SubClass < SuperClass
methods
function value = GetMyProp(obj)
value = 0;
end
end
end
Now, your subclass will always show 0 for MyProp, while the superclass keeps the correct value.
0 Comments
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!