Dependent property for subclasses
4 views (last 30 days)
Show older comments
Why does the following inheritance not work?
class A < handle
properties (Dependent)
a
end
methods
function this = A(a)
this.a = a;
end
function a = get.a(this)
a = this.a;
end
end
class B < A
methods
function this = B(a)
this.a = a;
end
function a = get.a(this)
a = this.a+1;
end
end
The error is:
The method get.a does not refer to a valid property name
0 Comments
Answers (2)
Sai Veeramachaneni
on 12 Mar 2021
Hi,
get.a is not a valid function name, Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscore only
Try changing it to getA instead of get.a
Hope it helps.
1 Comment
Steven Lord
on 12 Mar 2021
The first paragraph of your answer is true, but that syntax is valid in MATLAB. It is used to define a property accessor method for the a property. See this documentation page for more information.
Steven Lord
on 12 Mar 2021
You are only allowed to define a property accessor method for a property if
- the class in which the method is defined also defines the property
- the class does not include a properties block that defines that property but does allow dynamic properties (see the Set and Get Methods for Dynamic Properties documentation page linked in the See Also section), or
- the property is Abstract in its defining class, in which case a concrete subclass can define a property accessor method
No, a superclass defining a property in its properties block does not allow its subclasses to define a property accessor method. Doing so could allow the subclass to accept a value that would make the subclass instance not a valid superclass instance.
Consider a hierarchy where EquilateralTriangle inherits from Triangle. Triangle defines a property numSides and validates that this property always has value 3. [Yes, it should be a Constant property and not have a property accessor method, but this is a simplified example to illustrate a point.] If EquilateralTriangle could define a property accessor method that allows numSides to be 4 (due to a bug in the method) and you created an EquilateralTriangle with numSides equal to 4, it would not be a valid Triangle.
1 Comment
Captain Karnage
on 20 Apr 2023
One issue I'm having is if you use an Abstract property - you can't use validation functions in the property block. That gives the error:
Using size and validation functions on an inherited property is not supported.
What I want to do, is I'm creating a class with two integer values. I'm trying to make subclasses where the precision of the two values is different in each subclass - but they all share the same methods in the superclass so I only have to write the methods once (more importantly - only have to EDIT the methods once when I have to make changes). The idea is I have a superclass, let's call it DInteger.. and I want subclasses DInt, DInt8, DInt16, DInt32, and Dint64. The two properties are I1 and I2 (and also a logical named sign).
If I could do what I wanted, the superclass, DInteger would validate using {mustBeInteger} which will allow any integer type or a floating type that is equal to an integer - then DInt would set the type of I1 and I2 as double, DInt8 would set them to uint8, up to Dint64 which would set them to uint64. I can use whatever validators in the superclass, but can't add any validators in the subclass. I can use validation in my constructors and other functions that set the properties, but that doesn't guarantee the property doesn't get set to a wrong type somewhere. Is there anyway to do that?
See Also
Categories
Find more on Argument Definitions 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!