What exactly are Handle Compatible Classes? Are they a combination of handle and non-handle classes?

I read Handle Compatible Classes, the page say"Typically, when deriving a MATLAB® class from other classes, all the superclasses are handle classes, or none of them are handle classes. However, there are situations in which a class provides some utility that is used by both handle and nonhandle subclasses. Because it is not legal to combine handle and nonhandle classes, the author of the utility class must implement two distinct versions of the utility.
The solution is to use handle-compatible classes. You can use handle-compatible classes with handle classes when forming sets of superclasses. Designate a class as handle compatible by using the HandleCompatible class attribute."
I know “The HandleCompatible attribute is not inherited.”
I ran the following code,I found that the objects defined by A do not belong to any class! Why is this happening?
ta=A;
ty=isa(ta,'handle')
ty=isa(ta,'value')
ty=isa(ta,'HandleCompatible')
I ran the following code,tb do not belong to any class either! Why?
tb=B;
ty=isa(tb,'handle')
ty=isa(tb,'value')
ty=isa(tb,'HandleCompatible')
I ran the following code.Why can't I define objects for class C? I don't quite understand this error message.
c=C;
Error using C
If a class defines super-classes, all or none must be handle classes.
I have defined three classes, and the code is as follows:
classdef (HandleCompatible=true) A
properties
a
end
end
classdef B < A
properties
b
end
end
classdef C < B & handle
properties
c
end
end

 Accepted Answer

I found that the objects defined by A do not belong to any class! Why is this happening?
The syntax of of isa() is,
isa(object,classname)
but 'value' and "HandleCompatible' are not the names of classes. You cannot query whether an object is a value or handle compatible class this way.
Why can't I define objects for class C?
The handle compatibility of A is not inherited by B, see Handle Compatibility Rules

8 Comments

Thank you for your response. The syntax for identifying whether an object is of handle type is provided at the bottom of this page. Similarly, I assume that identifying whether it is of value class should use similar syntax, like isa(obj, 'type of class').
Additionally, why does 'The handle compatibility of A is not inherited by B' prevent class C from creating its own objects?
Thank you for your response. The syntax for identifying whether an object is of handle type is provided at the bottom of this page.
Yes, and note in particular where it says "The HandleCompatible attribute is not inherited." Therefore, B is not handle compatible. It is just a regular value class.
Similarly, I assume that identifying whether it is of value class should use similar syntax, like isa(obj, 'type of class').
No. See above.
Additionally, why does 'The handle compatibility of A is not inherited by B' prevent class C from creating its own objects?
Because B is a value class. A class (in this case C) cannot have a mixture of value and handle superclasses.
"Yes, and note in particular where it says "The HandleCompatible attribute is not inherited." Therefore, B is not handle compatible. It is just a regular value class.".
-----------It is my mistake,Thank you for pointing out my mistake.
"Because B is a value class. A class (in this case C) cannot have a mixture of value and handle superclasses."
------------Handle Compatible Classes said:"Because it is not legal to combine handle and nonhandle classes, ".
Based on the knowledge I've acquired so far, only handle classes and value classes have been introduced. Is the term 'nonhandle classes' referring to value classes? If not, what could nonhandle classes be? Could you provide an example?
handle compatible classes include (i) pure handle classes, and (ii) value classes declared as HandleCompatible. Handle compatible value classes can be mixed with pure handle classes udring inheritance. For example, you could have had,
classdef B < A | handle
properties
b
end
end
The parentage of B is legitimate here, because both its parents are handle compatible.
Can I consider Handle Compatible Classes as a third type of class, distinct from handle classes and value classes? Handle Compatible Classes can coexist with handle classes but not with value classes, and handle classes cannot coexist with value classes either?
You can mix handle compatible value classes with other value classes. The following is fine, for example,
classdef (HandleCompatible=true) myclass
end
classdef mysubclass<myclass & double
methods
function obj=mysubclass(data)
obj=obj@double(data);
end
end
end
B is value class(because “The HandleCompatible attribute is not inherited. )
So C is an illegal class because value classes and handle classes are not compatible. Am I correct in my understanding?
classdef C < B & handle
properties
c
end
end

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Asked:

on 28 Aug 2023

Commented:

on 30 Aug 2023

Community Treasure Hunt

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

Start Hunting!