MATLAB Object-Oriented Programming

(Joyce) #1
function delete(obj)
fclose(obj.FileID);
end

Destructors in Class Hierarchies


If you create a hierarchy of classes, each class can define its own delete method. When
destroying an object, MATLAB calls the delete method of each class in the hierarchy.
Defining a delete method in a handle subclass does not override the handle class
delete method. Subclass delete methods augment the superclass delete methods.

Inheriting a Sealed Delete Method

Classes cannot define a valid destructor that is Sealed. MATLAB returns an error when
you attempt to instantiate a class that defines a Sealed delete method.

Normally, declaring a method as Sealed prevents subclasses from overriding that
method. However, a Sealed method named delete that is not a valid destructor does
not prevent a subclass from defining its own destructor.

For example, if a superclass defines a method named delete that is not a valid
destructor, but is Sealed, then subclasses:


  • Can define valid destructors (which are always named delete).

  • Cannot define methods named delete that are not valid destructors.


Destructors in Heterogeneous Hierarchies

Heterogeneous class hierarchies require that all methods to which heterogeneous arrays
are passed must be sealed. However, the rule does not apply to class destructor methods.
Because destructor methods cannot be sealed, you can define a valid destructor in a
heterogeneous hierarchy that is not sealed, but does function as a destructor.

For information on heterogeneous hierarchies, see “Designing Heterogeneous Class
Hierarchies” on page 10-24

Object Lifecycle


MATLAB invokes the delete method when the lifecycle of an object ends. The lifecycle of
an object ends when the object is:

7 Value or Handle Class — Which to Use

Free download pdf