Property Attributes in Matlab Object Oriented Programming - Matlab OOP 14 | applied electronics engineering

# Property Attributes in Matlab Object Oriented Programming - Matlab OOP 14

By Applied Electronics - Saturday, February 11, 2017 No Comments
In this matlab object oriented programming tutorial we explain what property attributes in Matlab Object Oriented Programming are and how they set with examples.

This is the 14th tutorial of Object Oriented Programming with Matlab.

In the previous tutorial when talking about user defined classes, we created a class called player. It has two properties called name and age. One method was also declared which displayed these two properties. Once the object of the player class was created, we could see and modify the properties. Hence these two properties were public. The player class code is reshown below.

classdef player

properties
name;
age;
end

methods
function pObj = player(var1, var2)
if nargin == 2
pObj.name = var1;
pObj.age = var2;
end
end

function [] = disp(pObj)
if nargin > 0
fprintf('Name = %s \n', pObj.name);
fprintf('Age = %d \n', pObj.age);
end
end
end

end


As said, the name and age property were public because they could be read by anyone and could be modified to any other values by anyone. But what if you wanted to users not be able to see and modify the properties? What if you wished to make one property for example the name property visible but don't want to make the age property visible?

These idea of controlling readability and writeability of class properties is called property attributes. There are 3 property attributes as follows:
2. Private - read/write only by methods of the same class
3. Protected - read/write only by method of the same class or subclass of the property class
Thus the name and age properties in the above player class could be set to either public, private or protected. The keywords used to set these attributes are:
1. Access - make read/write property
2. GetAccess - make read property
3. SetAccess - make write property
For example let make the property name as public and lets make the property age as protected. The following code shows how to do this.

classdef player

properties
name;
end

properties(SetAccess = protected)
age;
end

methods
function pObj = player(var1, var2)
if nargin == 2
pObj.name = var1;
pObj.age = var2;
end
end

function [] = disp(pObj)
if nargin > 0
fprintf('Name = %s \n', pObj.name);
fprintf('Age = %d \n', pObj.age);
end
end
end

end


In the above code, we did not set any access control to the property name because the default attribute properties in matlab is public. So the property name is public. However, to make the age property protected we used the SetAccess control to set the property to protected. So in this way we can set the properties as public and protected. The rest of the code is the same as the default person class. Now that the player class property name is set to public we can read the name and we can also modify it. But since the player class property age is set to protected, we can read the age but we cannot modify it.

The following code illustrates this.

>> player1 = player;
>> player1.name

ans =

[]

>> player1.age

ans =

[]

>> player1.name = 'jon'

player1 =

Name = jon
Age =
>> player1.age = 20
You cannot set the read-only property 'age' of player.


In the code above, we created an object player1 of class player. Then we try to view the properties and then try to modify both the properties. As you can see the name property can be viewed as well as modified. However, we can only view the age property but we cannot change its value. Matlab Software throws error "You cannot set the read-only property 'age' of player.".

Similarly we can make the age property such that it not possible to read the age as well along with not able to modify it as before. This can be done by making the age property protected using the Access control. See the following code.

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

function [] = disp(pObj)
if nargin > 0
fprintf('Name = %s \n', pObj.name);
fprintf('Age = %d \n', pObj.age);
end
end
end

end


Now see what happens when we try to view the property age.

>> player1 = player;
>> player1.age
You cannot get the 'age' property of player.


In the same way you can also the make the properties private. The private attribute is similar to the protected. The difference is that if you make a property protected, it can be accessed using subclasses whereas private does not allow any subclasses to access the property. That is private attribute is more secure than protected.