Modify attributes of inherited property
Show older comments
I defined a subclass which inherits a built-in class (mechss) and extends it with some additional properties and methods. Because of my additional properties, the terminology becomes somewhat confusing and I would like to change the property names of the superclass. For example, I would like to change the property C to be called L.
As far as I understand, this is not possible, but I cán define the new property L and relate it to C using get and set methods. This leaves me with two identical properties however, and I would like to hide C and make its access protected.
Is something like this possible?
Accepted Answer
More Answers (1)
Sean de Wolski
on 2 Nov 2021
Edited: Sean de Wolski
on 2 Nov 2021
1 vote
You could instead author your own class that passes through to a mechss object it stores as a private property. Then you can call the properties whatever you want.
4 Comments
Luuk Poort
on 2 Nov 2021
That's not true, strictly speaking, as long as most of your method calls will be done with dot syntax obj.method(). If so, you could overload dot-indexing to make the method calls get forwarded to the mechss member. However, since you have to overload dot-indexing in either case, I don't think either approach involves less coding than the other.
Sean de Wolski
on 2 Nov 2021
The benefit of this approach is if MathWorks changes something in mechss (e.g. overloads indexing which it could), your solution won't break. And it's likely faster than overloading indexing.
Is the main challenge just naming confusion? If yes, maybe consider less confusing names, or names with a pattern/prefix that's easy to distinquish?
E.g., making things up
classdef electromechss < mechss
properties
electroC
electroL
end
methods
electroWhatever1
electroWhatever2
end
end
Luuk Poort
on 3 Nov 2021
Categories
Find more on Customize Object Indexing 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!