How do implement abstract properties without automatically instantiating them?
    21 views (last 30 days)
  
       Show older comments
    
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?
0 Comments
Answers (2)
  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!
  埃博拉酱
      
 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
  埃博拉酱
      
 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.
See Also
Categories
				Find more on Construct and Work with Object Arrays 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!

