Encapsulation
What is Encapsulation?
Encapsulation is one of the four main principles of Object-Oriented Programming (OOP). It refers to the practice of hiding the internal details of how an object works and only exposing a controlled interface to the outside world. In simpler terms, encapsulation means:
- Grouping data (attributes) and methods (functions that operate on the data) into a single unit (class).
- Making some data hidden (private) so that it cannot be accessed directly from outside the class.
- Providing methods to access or modify that hidden data in a safe and controlled manner.
This helps to protect the internal state of an object, ensures that only valid data is stored, and makes programs easier to maintain.
How to Achieve Encapsulation in EasyBite
In EasyBite, you can use the secret
keyword to make a class attribute private. This means the attribute cannot be accessed directly from outside the class. To interact with the private attribute, you can define methods known as getters and setters. These are regular methods that follow a naming convention (like getname
and setname
) but are not special built-in keywords.
Key Concepts
- Use
secret
to hide an attribute. - Use
method
to define a function that accesses (get) or modifies (set) the secret attribute. - Getters allow reading the value.
- Setters allow changing the value.
- Use
this
keyword to refer to the current object.
Example: Encapsulation in EasyBite
Below is a class that demonstrates how encapsulation works in EasyBite:
class User
secret password
init(p)
this.password to p
end init
method getpassword()
return this.password
end method
method setpassword(newpass)
this.password to newpass
end method
end class
set user1 to new User("secret123")
show user1.getpassword()
user1.setpassword("newpassword456")
show user1.getpassword()
Output
secret123
newpassword456
Why Not Access It Directly?
Trying to access the secret attribute directly will not work:
show user1.password
The code above will produce an error or be restricted by the EasyBite runtime because password
is marked as secret
. This is the core idea behind encapsulation: the internal data is hidden and protected from direct access.
Benefits of Encapsulation
- Security: Internal object data is protected from accidental or unauthorized access.
- Validation: You can add logic in setter methods to prevent invalid data.
method setpassword(newpass)
if newpass.length < 6
show "Password too short"
else
this.password to newpass
end if
end method
- Read-only and Write-only Access: You can create only a getter or only a setter, depending on the requirement.
- Maintenance: If the internal structure changes, you only need to update the methods, not the entire program.
- Control: Every interaction with secret data goes through a function, which allows adding logs, debugging, and enhancements easily.
Summary Table
Feature | EasyBite Keyword or Rule | Explanation |
---|---|---|
Define class | class | Groups data and methods |
Mark private attribute | secret | Hides the attribute from outside access |
Initialize values | init | Constructor to set initial values |
End block | end class , end method | Used to close class or method definitions |
Create object | new | Used to create a new instance of a class |
Access current object | this | Refers to the current instance |
Call method | object.method() | Invokes a method of an object |
Encapsulated access | Getter and Setter methods | Used to access or modify secret data safely |
Encapsulation is one of the first building blocks to writing well-structured, secure, and maintainable object-oriented programs. By using secret
along with methods, you can ensure your code is clean and professional.