Access Modifiers
In Object‑Oriented Programming, access modifiers control which parts of a class—its attributes and methods—are visible outside the class and which are hidden. EasyBite provides two levels of visibility:
- Public (default): accessible anywhere an object is used.
- Secret (private): accessible only within the class’s own methods and constructor, hidden from external code and subclasses.
This guide gives a very detailed, step‑by‑step look at access modifiers in EasyBite, with simple explanations, plenty of examples, and clear outputs so that even beginners can follow along.
Table of Contents
- Access Modifiers
- Table of Contents
- Why Access Modifiers Matter
- Public vs. Secret Visibility
- Declaring Public Attributes and Methods
- Declaring Secret Attributes and Methods
- Accessing Members
- Constructors and Secret Members
- Inheritance and Secret Members
- Common Pitfalls
- Best Practices
- Detailed Examples with Outputs
- Conclusion
Why Access Modifiers Matter
- Encapsulation: Hiding implementation details prevents external code from depending on internal state.
- Safety: Prevent accidental misuse or corruption of internal data.
- Clarity: Public API vs. internal mechanics—users of your class see only what they need.
By marking some members as secret, you make your class interface clean and robust.
Public vs. Secret Visibility
Visibility | Keyword | Accessible From | Use Case |
---|---|---|---|
Public | — | Anywhere | Core API: methods/attributes users interact with |
Secret | secret | Within the defining class only | Internal logic, helper methods, sensitive data |
- Public members require no keyword: every
set
andmethod
is public by default. - Secret members use the
secret
keyword beforeset
ormethod
.
Declaring Public Attributes and Methods
Public members form the class’s external interface. They are declared without any modifier:
class Account
set balance to 0 // public attribute
method deposit(amount)
set balance to balance + amount
end method
method getBalance()
return balance
end method
end class
balance
,deposit
, andgetBalance
are all public.- External code can read/write
balance
and call both methods.
Declaring Secret Attributes and Methods
Secret members are marked with secret
. They are invisible outside the class:
class Account
secret set password to "" // secret attribute
set balance to 0 // public attribute
init(pw)
set this.password to pw // okay: inside class
end init
method checkPassword(pw)
return pw == this.password
end method
secret method auditLog(entry)
// internal logging, not exposed publicly
show("Audit: " + entry)
end method
end class
password
andauditLog
are secret.- Only methods inside
Account
can access them.
Accessing Members
Public Access
set acct to new Account("s3cr3t")
acct.deposit(100)
show("Balance: " + acct.getBalance())
// Output:
// Balance: 100
Secret Access (Forbidden)
show(acct.password) // Error: 'password' is not accessible
acct.auditLog("X") // Error: 'auditLog' is not accessible
Secret members behave as if they do not exist from the outside.
Constructors and Secret Members
Constructors (init
… end init
) run in the class’s own scope, so they can set secret attributes:
class Person
secret set ssn to "" // Social Security Number, kept private
set name to ""
init(n, ssnValue)
set name to n
this.ssn to ssnValue // OK
end init
method getMaskedSSN()
// reveal only last 4 digits
return "****" + substring(this.ssn, 5, 8)
end method
end class
set p to new Person("Dana", "12345678")
show(p.getMaskedSSN())
// Output:
// ****5678
External code cannot read the full ssn
, only via the public getMaskedSSN()
.
Inheritance and Secret Members
Secret members are not visible to subclasses:
class Base
secret set secretCode to "XYZ"
method reveal()
return this.secretCode // OK in Base
end method
end class
class Derived inherit Base
method tryReveal()
// Attempting to access secretCode fails
return this.secretCode // Error: 'secretCode' is not accessible
end method
end class
Derived
cannot read or writesecretCode
.- Secret truly means private to the class where defined.
Common Pitfalls
- Forgetting
secret
: sensitive data remains public. - Attempting external access: causes runtime errors.
- Overusing secret: can hide useful functionality—use sparingly for truly internal members.
- Neglecting tests: secret methods are not directly testable; test via public API.
Best Practices
- Design clear public APIs: decide what users need, make everything else secret.
- Name secret members carefully: prefix with
_
in comments to indicate internal use (e.g.,_cache
). - Group secrets together at the top or bottom of the class for readability.
- Document public methods thoroughly; keep secret methods self‑explaining.
Detailed Examples with Outputs
Example 1: Secure Account
class SecureAccount
secret set password to ""
secret set failedAttempts to 0
set balance to 0
init(pw)
set this.password to pw
end init
method deposit(amount)
set balance to balance + amount
end method
method withdraw(amount, pw)
if pw == this.password then
set balance to balance - amount
this.failedAttempts to 0
return true
else
this.failedAttempts to this.failedAttempts + 1
return false
end if
end method
method getBalance(pw)
if pw == this.password then
return balance
else
return null
end if
end method
method isLockedOut()
return this.failedAttempts >= 3
end method
end class
set sa to new SecureAccount("letmein")
sa.deposit(200)
show(sa.withdraw(50, "wrong")) // Output: false
show(sa.withdraw(50, "wrong")) // Output: false
show(sa.withdraw(50, "wrong")) // Output: false
show(sa.isLockedOut()) // Output: true
show(sa.withdraw(50, "letmein")) // Output: false (account locked)
- All secret members (
password
,failedAttempts
) are hidden. - Public methods enforce access control.
Conclusion
Access modifiers in EasyBite—public (default) and secret—give you fine‑grained control over your class’s interface. By marking sensitive data and helper methods as secret, you:
- Protect internal state.
- Clarify the public API.
- Strengthen encapsulation.
Use secret
judiciously to keep your classes robust, maintainable, and secure.