How do implement abstract properties without automatically instantiating them?

14 views (last 30 days)
I am trying to have two abstract classes, in which one has the other as a property
classdef (Abstract) A
% Abstract class A
methods (Abstract)
methodA(obj)
end
end
classdef (Abstract) B
properties
aObj A
end
end
Now I am trying to make subclasses for both. A subclass for A is ok
classdef C < A
methods
function obj = C()
obj@A(); % Call the constructor of the superclass A
end
function methodA(obj)
disp('Method A implemented in class C');
end
end
end
But implementing a subclass for B is where problems begin
classdef D < B
% Class D that is a subclass of B
methods
function obj = D(inputA)
obj.aObj = inputA
end
function methodB(obj)
disp('Method B implemented in class D');
end
end
end
And this is the error I get:
Error defining property 'aObj' of class 'B'. Class A is abstract.
Specify a default value for property aObj.
It seems specifyign type for aObj forces it to be preconstructed before the constructor. How can I stop that and leave the abstract property to be properly initialiezd in the subclass's constructor?

Answers (2)

Sameer
Sameer on 11 Dec 2024
When you specify a property with a class type that is "abstract", MATLAB expects an instance of the property type to be provided or initialized, which can lead to issues since abstract classes cannot be instantiated directly.
To address this, you can use a workaround by defining the property as a more generic type and then enforcing the type constraint in the constructor or another method.
Here's how you can modify your classes:
classdef (Abstract) A
% Abstract class A
methods (Abstract)
methodA(obj)
end
end
classdef (Abstract) B
properties
aObj % Do not specify the type here
end
end
classdef C < A
methods
function obj = C()
obj@A(); % Call the constructor of the superclass A
end
function methodA(obj)
disp('Method A implemented in class C');
end
end
end
classdef D < B
methods
function obj = D(inputA)
if ~isa(inputA, 'A')
error('Input must be an instance of a subclass of A');
end
obj.aObj = inputA;
end
function methodB(obj)
disp('Method B implemented in class D');
end
end
end
Usage
% Create an instance of C
cInstance = C();
% Create an instance of D with cInstance as an input
dInstance = D(cInstance);
dInstance.methodB(); % Outputs: Method B implemented in class D
dInstance.aObj.methodA(); % Outputs: Method A implemented in class C
Hope this helps!
  1 Comment
Hasan Zakeri
Hasan Zakeri on 11 Dec 2024
Thanks!
But that is the whole point of abstract types, and with this "feature" they have made it pretty useless. I was trying to avoid a generic property. With the generic one, I may not even bother with abstraction!

Sign in to comment.


埃博拉酱
埃博拉酱 on 11 Dec 2024
Edited: 埃博拉酱 on 12 Dec 2024
According to the documentation, all properties of a class must have default values. If you don't specify a default value explicitly, MATLAB will attempt to construct a default value using the class you specified. However, abstract classes can't construct default values, so errors occur.
So the easiest way to do this is to explicitly specify a default value of a concrete class.
classdef (Abstract) B
properties
aObj A=C.empty
end
end
20241212
MATLAB stipulates that all properties must have default values. What do you want it to default in your design?
For example, is it possible not to specify A as an abstract class, but its constructor does nothing, just representing a null value?
  2 Comments
Hasan Zakeri
Hasan Zakeri on 12 Dec 2024
Thanks.
I have many different classes that are subclasses of A and not just C, that why I need to avoid having C in the definition of the the abstract class (C is one of many possible definitions for A)
埃博拉酱
埃博拉酱 on 12 Dec 2024
Edited: 埃博拉酱 on 12 Dec 2024
@Hasan Zakeri MATLAB stipulates that all properties must have default values. What do you want it to default in your design?
For example, is it possible not to specify A as an abstract class, but its constructor does nothing, just representing a null value? If you don't want the user to construct an empty A, you can also set the constructor's Access to be open to only a few specific classes that you define.
Or you can define a subclass derived from A specifically to represent nulls/defaults.

Sign in to comment.

Categories

Find more on Class Introspection and Metadata in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!