Documentation Center

  • Trial Software
  • Product Updates

Controlling Access to Class Members

Basic Knowledge

The material presented in this section builds on an understanding of the following information:

 Related Topics

 Terminology and Concepts

 Possible Values for Access to Class Members

Applications for Access Control Lists

Access control lists enable you to control access to specific class properties, methods, and events, by specifying a list of classes to which you want to grant access to these class members.

This technique provides greater flexibility and control in the design of a system of classes. For example, use access control lists when you want to define parts of your class system in separate classes, but do not want to allow access to class members from outside the class system.

Specify Access to Class Members

Specify the classes that are allowed to access a particular class member in the member access attribute statement. For example:

methods (Access = {?ClassName1,?ClassName2,...})

Use the class meta.class object to refer to classes in the access list. To specify more than one class, use a cell array of meta.class objects. Use the package name when referring to classes that are in packages.

    Note:   You must specify the meta.class objects explicitly (created with the ? operator), not as values returned by functions or other MATLAB expressions.

 Property Access

 Method Access

 Event Access

How MATLAB Interprets Attribute Values

  • Granting access to a list of classes restricts access to only:

    • The defining class

    • The classes in the list

    • Subclasses of the classes in the list

  • Including the defining class in the access list gives all subclasses of the defining class access.

  • MATLAB resolves references to classes in the access list only when the class is loaded. If MATLAB cannot find a class that is included in the access list, that class is effectively removed from the list.

  • An empty access list (that is, an empty cell array) is equivalent to private access.

Specifying Metaclass Objects

Use only the ? operator and the class name to generate the meta.class objects. Values assigned to the attributes cannot contain any other MATLAB expressions, including functions that return allowed attribute values:

  • meta.class objects

  • Cell arrays of meta.class objects

  • The values public, protected, or private

You must specify these values explicitly, as shown in the example code in this section.

Properties with Access Lists

These sample classes show the behavior of a property that grants read access (GetAccess) to a class. The GrantAccess class gives GetAccess to the NeedAccess class for the Prop1 property:

classdef GrantAccess
   properties (GetAccess = ?NeedAccess)
      Prop1 = 7;
   end
end

The NeedAccess class defines a method that uses the value of the GrantAccess Prop1 value. The dispObj is defined as a Static method, however, it could be an ordinary method.

classdef NeedAccess
   methods (Static)
      function dispObj(GrantAccessObj)
         % Display the value of Prop1
         disp(['Prop1 is: ',num2str(GrantAccessObj.Prop1)])
      end
   end
end

Get access to Prop1 is private so MATLAB returns an error:

>> a = GrantAccess;
>> a.Prop1
Getting the 'Prop1' property of the 'GrantAccess' class is not allowed.

However, MATLAB allows access to Prop1 by the NeedAccess class:

>> NeedAccess.dispObj(a)
Prop1 is: 7

Methods with Access Lists

Classes granted access to a method can:

  • Call the method using an instance of the defining class.

  • Define their own method with the same name (if not a subclass).

  • Override the method in a subclass only if the superclass defining the method includes itself or the subclass in the access list.

These sample classes show the behavior of methods called from methods of other classes that are in the access list. The class AcListSuper gives the AcListNonSub class access to itsm1 method:

classdef AcListSuper
   methods (Access = {?AcListNonSub})
      function obj = m1(obj)
         disp ('Method m1 called')
      end
   end
end

Because AcListNonSub is in the access list of m1, its methods can call m1 using an instance of AcListSuper:

classdef AcListNonSub
   methods
      function obj = nonSub1(obj,AcListSuper_Obj)
         % Call m1 on AcListSuper class
         AcListSuper_Obj.m1;
      end
      function obj = m1(obj)
         % Define a method named m1
         disp(['Method m1 defined by ',class(obj)])
      end
   end
end

Create objects of both classes:

>> a = AcListSuper;
>> b = AcListNonSub;

Call the AcListSuper m1 method using an AcListNonSub method:

>> b.nonSub1(a);
Method m1 called

Call the AcListNonSub m1 method:

>> b.m1;
Method m1 defined by AcListNonSub

Subclasses Without Access

Including the defining class in the access list for a method grants access to all subclasses derived from that class. When you derive from a class that has a method with an access list and that list does not include the defining class in the access list:

  • Subclass methods cannot call the superclass method because it is effectively private.

  • Subclasses cannot override the superclass method.

  • Subclass methods can call the superclass method indirectly using an instance of a class that is in the access list.

  • Nonsubclass methods of classes in the superclass method access list can call the superclass method using an instance of a subclass that is not in the superclass method access list.

For example, AcListSub is a subclass of AcListSuper. The AcListSuper class defines an access list for method m1. However, this list does not include AcListSuper, which would implicitly include all subclasses of AcListSuper in the access list:

classdef AcListSub < AcListSuper
   methods
      function obj = sub1(obj,AcListSuper_Obj)
         % Access m1 via superclass object (NOT ALLOWED)
         AcListSuper_Obj.m1;
      end
      function obj = sub2(obj,AcListNonSub_Obj,AcListSuper_obj)
         % Access m1 via object that is in access list (is allowed)
         AcListNonSub_Obj.nonSub1(AcListSuper_Obj);
      end
   end
end

Attempting to call the superclass m1 method results in an error because subclasses are not in the access list for the method:

>> a = AcListSuper;
>> b = AcListNonSub;
>> c = AcListSub;
>> c.sub1(a);
Error using AcListSuper/m1
Cannot access method 'm1' in class 'AcListSuper'.

Error in AcListSub/sub1 (line 4)
         AcListSuper_Obj.m1;

The AcListSub sub2 method can call a method of a class that is on the access list for m1, and that method (nonSub1) does have access to the superclass m1 method:

>> c.sub2(b,a);
Method m1 called

When subclasses are not included in the access list for a method, those subclasses cannot define a method with the same name. This behavior is not the same as cases in which the method's Access is explicitly declared as private.

For example, adding the following method to the AcListSub class definition produces an error when you attempt to instantiate the class.

methods (Access = {?AcListNonSub})
   function obj = m1(obj)
      disp('AcListSub m1 method')
   end
end

If you attempt to instantiate the class, MATLAB returns an error:

>> c = AcListSub;
Error using AcListSub
Class 'AcListSub' is not allowed to override the method 'm1' because neither it nor its
superclasses have been granted access to the method by class 'AcListSuper'.

The AcListNonSub class, which is in the m1 method access list, can define a method that calls the m1 method using an instance of the AcListSub class. While AcListSub is not in the access list for method m1, it is a subclass of AcListSuper.

For example, add the following method to the AcListNonSub class:

methods
   function obj = nonSub2(obj,AcListSub_Obj)
      disp('Call m1 via subclass object:')
      AcListSub_Obj.m1;
   end
end

Calling the nonSub2 method results in execution of the superclass m1 method:

>> b = AcListNonSub;
>> c = AcListSub;
>> b.nonSub2(c);
Call m1 via subclass object:
Method m1 called

This is consistent with the behavior of any subclass object, which can be substituted for an instance of its superclass.

Abstract Methods with Access Lists

A class containing a method declared as Abstract is an abstract class. It is the responsibility of subclasses to implement the abstract method using the function signature declared in the class definition.

When an abstract method has an access list, only the classes in the access list can implement the method. A subclass that is not in the access list cannot implement the abstract method so that subclass is itself abstract.

Was this topic helpful?