Documentation

Class Components

Class Building Blocks

MATLAB® organizes class definition code into modular blocks, delimited by keywords. All keywords have an associated end statement:

  • classdef...end — Definition of all class components

  • properties...end — Declaration of property names, specification of property attributes, assignment of default values

  • methods...end — Declaration of method signatures, method attributes, and function code

  • events...end — Declaration of event name and attributes

  • enumeration...end — Declaration of enumeration members and enumeration values

properties, methods, events, and enumeration are keywords only within a classdef block.

Class Definition Block

The classdef block contains the class definition within a file that starts with the classdef keyword and terminates with the end keyword.

classdef (ClassAttributes) ClassName < SuperClass
   ...
end

For example, this classdef defines a class called MyClass that sublcasses the handle class, but cannot be used to derive subclasses:

classdef (Sealed) MyClass < handle
   ...
end

For more syntax information, see Classdef Block.

Properties Block

The properties block (one for each unique set of attribute specifications) contains property definitions, including optional initial values. The properties block starts with the properties keyword and terminates with the end keyword.

classdef ClassName
   properties (PropertyAttributes)
      ...
   end
   ...
end

For example, this class defines a property called Prop1 that has private access and has a default value equal to the output of the date function.

classdef MyClass
   properties (Access = private)
      Prop1 = date;
   end
   ...
end

For more information, see Properties.

Methods Block

The methods block (one for each unique set of attribute specifications) contains function definitions for the class methods. The methods block starts with the methods keyword and terminates with the end keyword.

classdef ClassName
   methods (MethodAttributes)
      ...
   end
   ...
end

For example:

classdef MyClass
   methods (Access = private)
      function obj = myMethod(obj)
      ...
      end
   end
end

For more information, see Methods and Functions.

Events Block

The events block (one for each unique set of attribute specifications) contains the names of events that this class declares. The events block starts with the events keyword and terminates with the end keyword.

classdef ClassName
   events (EventAttributes)
      EventName
   end
   ...
end

For example, this class defined an event called StateChange with a ListenAccess set to protected:

classdef EventSource
   events (ListenAccess = protected)
      StateChanged
   end
   ...
end

For more information, see Events and Listeners.

Enumeration Block

The enumeration block contains the enumeration members defined by the class. The enumeration block starts with the enumeration keyword and terminates with the end keyword.

classdef ClassName < SuperClass
   enumeration
      EnumerationMember
   end
   ...
end

For example, this class defines two enumeration members that represent logical false and true:

classdef Boolean < logical
   enumeration
      No  (0)
      Yes (1)
   end
end

For more information, see Working with Enumerations.

Related Information

Class and Path Folders

Was this topic helpful?