This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Class Members Access

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. Access control lists specify a list of classes to which you 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 to define separate classes, but not 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:   Specify the meta.class objects explicitly (created with the ? operator), not as values returned by functions or other MATLAB expressions.

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 access.

  • MATLAB replaces unresolved meta.class entries in the list with empty meta.class objects.

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

Specify Metaclass Objects

Generate the meta.class objects using only the ? operator and the class name. 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

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

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

classdef NeedAccess
   methods (Static)
      function dispObj(GrantAccessObj)
         disp(['Prop1 is: ',num2str(GrantAccessObj.Prop1)])

Get access to Prop1 is private so MATLAB returns an error if you attempt to access the property from outside the class definition. For example, from the command line:

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

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

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')

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

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

Create objects of both classes:

a = AcListSuper;
b = AcListNonSub;

Call the AcListSuper m1 method using an AcListNonSub method:

Method m1 called

Call the AcListNonSub m1 method:

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:

  • Subclass methods cannot call the superclass method.

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

  • Subclasses cannot override the superclass method.

  • Methods of classes that are in the superclass method access list, but that are not subclasses, can call the superclass method.

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, so subclasses of AcListSuper do not have access to method m1:

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

No Direct Call to Superclass Method

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

a = AcListSuper;
c = AcListSub;
Cannot access method 'm1' in class 'AcListSuper'.

Error in AcListSub/sub1 (line 4)

Indirect Call to Superclass Method

You can call a superclass method from a subclass that does not have access to that method using an object of a class that is in the superclass method access list.

The AcListSub sub2 method calls a method of a class (AcListNonSub) that is on the access list for m1. This method, nonSub1, does have access to the superclass m1 method:

a = AcListSuper;
b = AcListNonSub;
c = AcListSub;
Method m1 called

No Redefining Superclass Method

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 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')
c = 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'.

Call Superclass from Listed Class Via Subclass

The AcListNonSub class is in the m1 method access list. This class can define a method that calls the m1 method using an object 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:

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

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

b = AcListNonSub;
c = AcListSub;
Call m1 via subclass object:
Method m1 called

This behavior is consistent with the behavior of any subclass object, which can substitute for an object 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.

Related Topics

Was this topic helpful?