Access control helps you decide who can use or change parts of your program. It keeps your data safe and organized.
Access control in MATLAB
classdef ClassName
properties (Access = public) % or private or protected
% variables here
end
methods (Access = public) % or private or protected
% functions here
end
endpublic means anyone can use it.
private means only inside the class can use it.
protected means the class and its subclasses can use it.
secretNumber so it can't be changed from outside. You can only get it using getSecret.classdef MyClass
properties (Access = private)
secretNumber
end
methods
function obj = MyClass(num)
obj.secretNumber = num;
end
function n = getSecret(obj)
n = obj.secretNumber;
end
end
endvisibleNumber that anyone can read or change.classdef MyClass
properties (Access = public)
visibleNumber
end
endprotectedData can be used inside ParentClass and any class that inherits from it.classdef ParentClass
properties (Access = protected)
protectedData
end
endThis program creates a BankAccount class with a private balance. You can only change the balance by using deposit and withdraw methods. Trying to change balance directly from outside causes an error.
classdef BankAccount
properties (Access = private)
balance
end
methods
function obj = BankAccount(initialAmount)
obj.balance = initialAmount;
end
function obj = deposit(obj, amount)
obj.balance = obj.balance + amount;
end
function obj = withdraw(obj, amount)
if amount <= obj.balance
obj.balance = obj.balance - amount;
else
disp('Not enough balance');
end
end
function b = getBalance(obj)
b = obj.balance;
end
end
end
% Using the class
account = BankAccount(100);
account = account.deposit(50);
account = account.withdraw(30);
disp(['Balance: ', num2str(account.getBalance())]);
account.balance = 1000; % This will cause an error because balance is privatePrivate properties and methods cannot be accessed from outside the class.
Use public methods to safely interact with private data.
Protected access is useful when you plan to create child classes.
Access control keeps parts of your program safe and organized.
Use private to hide data inside a class.
Use public to allow access from anywhere.
Use protected to share with child classes only.
Practice
private access modifier do in a MATLAB class?Solution
Step 1: Understand the role of
Theprivatein MATLAB classesprivatekeyword restricts access to properties or methods so that only the class itself can use them.Step 2: Compare with other access modifiers
publicallows access from anywhere,protectedallows access from child classes, butprivateis the most restrictive.Final Answer:
It restricts access to class properties or methods only within the class itself. -> Option DQuick Check:
privatemeans class-only access [OK]
- Confusing private with public access
- Thinking private allows child class access
- Assuming private is accessible from outside
data in a MATLAB class?Solution
Step 1: Recall MATLAB property access syntax
Access modifiers are specified in parentheses after thepropertieskeyword, e.g.,properties (protected).Step 2: Identify correct syntax for protected
The correct syntax uses parentheses:properties (protected). properties protected data end misses parentheses, so it's invalid.Final Answer:
properties (protected) data end -> Option BQuick Check:
Use parentheses for access modifiers [OK]
- Omitting parentheses around access modifier
- Using wrong access modifier for the question
- Placing access modifier outside properties block
classdef MyClass
properties (Access = private)
secret = 42;
end
methods
function val = getSecret(obj)
val = obj.secret;
end
end
end
obj = MyClass();
disp(obj.secret);
What happens when you run this code?Solution
Step 1: Identify access level of
The propertysecretsecretis declared private, so it cannot be accessed directly outside the class.Step 2: Analyze the code trying to access
The linesecretdisp(obj.secret);tries to access the private property directly, which is not allowed and causes an error.Final Answer:
It causes an error becausesecretis private. -> Option AQuick Check:
Private properties block outside access [OK]
- Assuming private properties can be accessed directly
- Confusing method access with property access
- Ignoring access modifiers in property declaration
classdef Parent
properties (Access = protected)
value
end
end
classdef Child < Parent
methods
function setValue(obj, val)
obj.value = val;
end
end
end
c = Child();
c.setValue(10);
disp(c.value);Solution
Step 1: Understand protected access
The propertyvalueis protected, so Child can set it, but outside code cannot access it directly.Step 2: Identify why
Outside code tries to accessdisp(c.value)failsvaluedirectly, which is not allowed for protected properties.Step 3: Fix by adding a getter method
Adding a method in Child to returnvalueallows controlled access without changing access level.Final Answer:
Add a getter method in Child to accessvalue. -> Option AQuick Check:
Use getter for protected property access outside class [OK]
- Trying to access protected property directly outside class
- Changing access to public unnecessarily
- Not using inheritance properly
BankAccount where the balance is private, but child classes can update it. You also want to allow anyone to check the balance. Which access control setup is correct?Solution
Step 1: Identify access needs for
Balance should be hidden from outside for updates but readable by anyone, and updatable by child classes.balanceStep 2: Choose access modifier and methods
Protected allows child classes to update balance. A public getter method allows anyone to read balance safely.Final Answer:
Declarebalanceas protected and provide a public getter method; child classes can update balance, others can only read it. -> Option CQuick Check:
Protected + public getter fits requirements [OK]
- Making balance public allowing unwanted updates
- Using private without getter blocking read access
- Not providing a getter method for balance
Ready to prove it?
Pass the test and this pattern moves unknown
