Why not possible to use property class validation with abstract class?

22 views (last 30 days)
In the code below class fruit_t has an abstract method that is implemented by class banana_t.
I can not instantiate a fruit_t, but I can instantiate a banana_t.
But why can I not use fuit_t for property class validation in another top class top_t?
Error message is
Error defining property 'fruit' of class 'top_t'. Class fruit_t is abstract. Specify a default value for
property fruit.
As property fruit is a "immutable" property of top_t it will be set in the contructor, so why does Matlab need another default value?
If fruit_t is not abstract and fruit is not assigned a value in the constructur then property fruit will be of type
fruit: [0×0 fruit_t]
Why can't matlab think the same way with abstract classes then, since there is not created instance of fruit_t.
My suggestion:
  1. Either don't require a "default value" for immutable properties (because they will be set in the constructor), or
  2. Allow zero element arrays of abstract classes (since there are no instances, there is no problem).
classdef top_t < handle
properties (SetAccess = immutable)
fruit fruit_t
end
methods
function e = top_t()
e.fruit = banana_t();
end
end
end
classdef fruit_t < handle
methods (Abstract)
y = foo(x);
end
end
classdef banana_t < fruit_t
methods
function y = foo(e, x)
y = 2*x;
end
end
end

Answers (1)

Shubham
Shubham on 16 Feb 2024
Hi Jim,
In MATLAB, when you define an abstract class, you are essentially creating a template for other classes to follow. This template specifies methods that must be implemented by any non-abstract subclasses. However, because an abstract class is only a template, you cannot create instances of it directly.
The error message you're seeing is due to MATLAB requiring a concrete class for property validation when the property is immutable. Immutable properties in MATLAB must be assigned a value when the object is constructed and cannot be changed afterward. MATLAB's type system expects that any property, including immutable ones, has a valid default value even before the constructor is called, which ensures that all properties have valid types at all times.
When you declare a property of an abstract class type without providing a default value, MATLAB does not have a concrete class to instantiate for the default value. Since abstract classes cannot be instantiated, MATLAB cannot assume a zero-element array as the default value for an abstract class property, unlike with non-abstract classes, where it can create a zero-element array by default.
Your suggestion to allow zero element arrays of abstract classes could be a potential solution, but it might conflict with MATLAB's type safety rules, as it would represent an array of a class that cannot be instantiated.
To resolve the issue, you can provide a default value for the fruit property in the top_t class that is a concrete instance of a non-abstract subclass of fruit_t, or you can modify the design to not use an abstract class for the property if the property needs to be immutable and no suitable default can be provided.
Here's a modified version of your code that provides a default value for the fruit property:
classdef top_t < handle
properties (SetAccess = immutable)
fruit fruit_t = banana_t(); % Providing a default value that is a concrete instance
end
methods
function e = top_t(fruitInstance)
if nargin > 0 % Check if a constructor argument was provided
e.fruit = fruitInstance;
end
% If no argument was provided, the default value banana_t() is used
end
end
end
classdef fruit_t < handle
methods (Abstract)
y = foo(x);
end
end
classdef banana_t < fruit_t
methods
function y = foo(e, x)
y = 2*x;
end
end
end
In this code, the fruit property is given a default value of banana_t(), which is a concrete instance of a subclass of fruit_t. When creating an instance of top_t, you can optionally pass a different fruit_t subclass instance to the constructor to override the default value. If no argument is provided, the default banana_t instance is used. This approach satisfies MATLAB's requirement for a default value for immutable properties.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!