Main Content

Behavior of Inherited Built-In Methods

Subclass double

Most built-in functions used with built-in classes are actually methods of the built-in class. For example, the double and single classes define several methods to perform arithmetic operations, indexing, matrix operation, and so on. All these built-in class methods work with subclasses of the built-in class.

Subclassing double enables your class to use features without implementing the methods that a MATLAB® built-in class defines.

The DocSimpleDouble class subclasses the built-in double class.

classdef DocSimpleDouble < double
   methods
      function obj = DocSimpleDouble(data)
         if nargin == 0
            data = 0;
         end
         obj = obj@double(data);
      end
   end
end

Create an instance of the class DocSimpleDouble.

sc = DocSimpleDouble(1:10)
sc = 
   1x10 DocSimpleDouble:
  double data:
     1     2     3     4     5     6     7     8     9    10

Call a method inherited from class double that operates on the data, such as sum. sum returns a double and, therefore, uses the display method of class double:

sum(sc)
ans =
    55

Index sc like an array of doubles. The returned value is the class of the subclass:

a = sc(2:4)
a = 
  1x3 DocSimpleDouble:
  double data:
     2     3     4

Indexed assignment works the same as the built-in class. The returned value is the class of the subclass:

sc(1:5) = 5:-1:1
sc = 
   1x10 DocSimpleDouble:
  double data:
     5     4     3     2     1     6     7     8     9    10

Calling a method that modifies the order of the data elements operates on the data, but returns an object of the subclass:

sc = DocSimpleDouble(1:10);
sc(1:5) = 5:-1:1;
a = sort(sc)
a = 
   1x10 DocSimpleDouble:
  double data:
     1     2     3     4     5     6     7     8     9    10

Built-In Data Value Methods

When you call a built-in data value method on a subclass object, MATLAB uses the superclass part of the subclass object as inputs to the method. The value returned is same class as the built-in class. For example:

sc = DocSimpleDouble(1:10);
a = sin(sc);
class(a)
ans =

double

Built-In Data Organization Methods

This group of built-in methods reorders or reshapes the input argument array. These methods operate on the superclass part of the subclass object, but return an object of the same type as the subclass.

sc = DocSimpleDouble(randi(9,1,10))
sc = DocSimpleDouble(randi(9,1,10))

sc = 

  1x10 DocSimpleDouble:

  double data:
     6     1     8     9     7     7     7     4     6     2
b = sort(sc)
b = 

  1x10 DocSimpleDouble:

  double data:
     1     2     4     6     6     7     7     7     8     9

Methods in this group include:

Built-In Indexing Methods

Built-in classes use specially implemented versions of the subsref, subsasgn, and subsindex methods to implement indexing. When you index a subclass object, only the built-in data is referenced (not the properties defined by your subclass).

For example, indexing element 2 in the DocSimpleDouble subclass object returns the second element in the vector:

sc = DocSimpleDouble(1:10);
a = sc(2)
a = 
  DocSimpleDouble
  double data:
     2

The value returned from an indexing operation is an object of the subclass. You cannot make indexed references if your subclass defines properties, unless your subclass overrides the default subsref method.

Assigning a new value to the second element in the DocSimpleDouble object operates only on the superclass data:

sc(2) = 12
sc = 
   1x10 DocSimpleDouble:
  double data:
     1    12     3     4     5     6     7     8     9    10

The subsref method also implements dot notation for methods.

Built-In Concatenation Methods

Built-in classes use the functions horzcat, vertcat, and cat to implement concatenation. When you use these functions with subclass objects of the same type, MATLAB concatenates the superclass data to form a new object. For example, you can concatenate objects of the DocSimpleDouble class:

sc1 = DocSimpleDouble(1:10);
sc2 = DocSimpleDouble(11:20);
[sc1,sc2]
ans = 
  1x20 DocSimpleDouble:
  double data:
  Columns 1 through 13
     1     2     3     4     5     6     7     8     9    10    11    12    13
  Columns 14 through 20
    14    15    16    17    18    19    20
[sc1;sc2]
ans = 
  2x10 DocSimpleDouble:
  double data:
     1     2     3     4     5     6     7     8     9    10
    11    12    13    14    15    16    17    18    19    20

Concatenate two objects along a third dimension:

c = cat(3,sc1,sc2)
c = 

  1x10x2 DocSimpleDouble:

  double data:

(:,:,1) =

     1     2     3     4     5     6     7     8     9    10


(:,:,2) =

    11    12    13    14    15    16    17    18    19    20

If the subclass of a built-in class defines properties, you cannot concatenate objects of the subclass. There is no way to determine how to combine properties of different objects. However, your subclass can define custom horzcat and vertcat methods to support concatenation in whatever way makes sense for your subclass.

Related Topics