Main Content

Handle Compatible Classes

Basic Knowledge

The material presented in this section builds on knowledge of the following information.

Key Concepts

Handle-compatible class — a class that you can include with handle classes in a class hierarchy, even if the class is not a handle class.

  • All handle classes are handle-compatible.

  • All superclasses of handle-compatible classes must also be handle compatible.

HandleCompatible — the class attribute that defines nonhandle classes as handle compatible.

When to Use Handle-Compatible Classes

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.

classdef (HandleCompatible) MyClass
   ...
end

Handle Compatibility Rules

Handle-compatible classes (that is, classes whose HandleCompatible attribute is set to true) follow these rules:

  • All superclasses of a handle-compatible class must also be handle compatible

  • If a class explicitly sets its HandleCompatibility attribute to false, then none of the class superclasses can be handle classes.

  • If a class does not explicitly set its HandleCompatible attribute and, if any superclass is a handle, then all superclasses must be handle compatible.

  • The HandleCompatible attribute is not inherited.

A class that does not explicitly set its HandleCompatible attribute to true is:

  • A handle class if any of its superclasses are handle classes

  • A value class if none of the superclasses are handle classes

Identify Handle Objects

To determine if an object is a handle object, use the isa function:

isa(obj,'handle')

Related Topics