Method Attributes in Matlab Object Oriented Programming - Matlab OOP 15 | applied electronics engineering

Latest

Method Attributes in Matlab Object Oriented Programming - Matlab OOP 15

By Applied Electronics - Sunday, February 12, 2017 No Comments
A class methods can have public, private or protected attributes. That is like properties attributes you can limit the access to class methods by setting them either to public, private or protected. Public is the default method attribute meaning that if you don't define the access attribute to a method then it is public by default and everyone can use the method or call the method.

In the last tutorial Property Attributes in Matlab Object Oriented Programming we explained the property attributes, the different properties attributes types and how to use them. In this matlab object oriented programming tutorial we describe the different method attributes and how to use them. To see the complete tutorial see object oriented programming with Matlab.

There are several methods attributes. The attributes that limits the access to the methods are the followings:
  1. public, the method can be accessed from anywhere and anyone
  2. private, the method can be accessed only by methods within the same class
  3. protected, the method can be accessed only by the methods within the same class or the sub-classes
The public access method attribute is used when you don't want to restrict the use of the method. You can declare a method within a class as public using parenthesis. For example using the previous player user defined class as an example, the disp() method has public attribute by default and it was not necessary to use the public keyword. But you can add the public access keyword as follows.

classdef player
    
    properties
        name;
    end
    
    properties(Access = protected)
        age;
    end
    
    methods
        function pObj = player(var1, var2)
            if nargin == 2
                pObj.name = var1;
                pObj.age = var2;
            end
        end
    end
    
    methods(Access = public)
        
        function [] = disp(pObj)
            if nargin > 0
                fprintf('Name = %s \n', pObj.name);
                fprintf('Age = %d \n', pObj.age); 
            end
        end
    end
    
end

As you can see we have set the methods block which uses the disp() method as public by writing Access = public in parenthesis after the methods keyword.

Now by creating an object player1 of class player the disp methods gets called and since it is public the Name and Age properties due to fprintf statements gets executed. This is shown below.

>> player1 = player

player1 = 

Name =  
Age = 


Similarly you can restrict the access to the same method disp by using keyword private or protected. For example in the following the disp is made a private access method.

classdef player
    
    properties
        name;
    end
    
    properties(Access = protected)
        age;
    end
    
    methods
        function pObj = player(var1, var2)
            if nargin == 2
                pObj.name = var1;
                pObj.age = var2;
            end
        end
    end
    
    methods(Access = private)
        
        function [] = disp(pObj)
            if nargin > 0
                fprintf('Name = %s \n', pObj.name);
                fprintf('Age = %d \n', pObj.age); 
            end
        end
    end
    
end 

Now since the disp method is set to private access when an object is created Matlab will not display the Name and Age due to the disp method:

>> player1 = player

player1 = 


player1 = 

Error using player/display
Cannot access method 'disp' in class 'player'. 

Other methods attributes supported by Matlab Software are the followings:

  1. Abstract
  2. Hidden
  3. Sealed
  4. Static
These method attributes are logical attributes meaning they can be set to true or false. By default they are false. The abstract attribute is used to enable(true) or disable(false) the method.

The Hidden method attribute is used to either hide or show the method when the methods of the class being inquired using the methods() function. The methods are shown by default which means the Hidden attribute is set to false.

The Sealed method attribute is used to limit the modification of the method by a subclass. It is false by default meaning that a subclass can modify the method. However if this is set to True then the method cannot be modified by the subclass.

The Static method attribute is used for methods which are usable by all objects and not to any particular objects. Methods which provides constants or just conversion formula etc are usually declared static By default, the methods are not static. To make a method static it must be set to True.


No Comment to " Method Attributes in Matlab Object Oriented Programming - Matlab OOP 15 "