Main Content

The Handle Superclass

Building on the Handle Class

The handle class is an abstract class. Therefore, you cannot create objects of this class directly. Use the handle class as a superclass to implement subclasses that inherit handle behavior. MATLAB® defines several classes that derive from the handle class. These classes provide specialized functionality to subclasses.

Specialized Handle Base Classes

To add both handle behavior and specific functionality to your class, derive your class from these handle classes:

  • matlab.mixin.SetGet — Provides set and get methods to access property values.

  • dynamicprops — Enables you to define properties that are associated with an object, but not the class in general.

  • matlab.mixin.Copyable Provides a copy method that you can customize for your class.

For information on how to define subclasses, see Design Subclass Constructors.

Handle Class Methods

When you derive a class from the handle class, the subclass inherits methods that enable you to work more effectively with handle objects.

List the methods of a class by passing the class name to the methods function:

Methods for class handle:

addlistener  findobj      gt           lt           
delete       findprop     isvalid      ne           
eq           ge           le           notify  

Event and Listener Methods

For information on how to use the notify and addlistener methods, see Events and Listeners Syntax.

Relational Methods

TF = eq(H1,H2)
TF = ne(H1,H2)
TF = lt(H1,H2)
TF = le(H1,H2)
TF = gt(H1,H2)
TF = ge(H1,H2)

The handle class overloads these functions to support equality tests and sorting on handles. For each pair of input arrays, these functions return a logical array of the same size. Each element is an element-wise equality or comparison test result. The input arrays must be the same size or one (or both) can be scalar. The method performs scalar expansion as required. For more information on handle class relational methods, see relationaloperators.

Test Handle Validity

Use the isvalid handle class method to determine if a variable is a valid handle object. For example, in the statement:

B = isvalid(H)

B is a logical array in which each element is true if, and only if, the corresponding element of H is a valid handle. B is always the same size as H.

When MATLAB Destroys Objects

MATLAB destroys objects in the workspace of a function when the function:

  • Reassigns an object variable to a new value

  • Does not use an object variable for the remainder of a function

  • Function execution ends

When MATLAB destroys an object, it also destroys values stored in the properties of the object. MATLAB frees computer memory associated with the object for use by MATLAB or the operating system.

You do not need to free memory in handle classes. However, there can be other operations that you want to perform when destroying an object. For example, closing a file or shutting down an external program that the object constructor started. Define a delete method in your handle subclass for these purposes.

See Handle Class Destructor for more information.

Related Topics