02 Encapsulation
02 Encapsulation
Programming
Week 02 – Encapsulation
LECTURES:
• SAMUEL ADY SANJAYA
• AGUS SULAIMAN
• BUDI SITORUS
• ENRICO SISWANTO
Objectives
Importance:
• Ensures data integrity by hiding the
internal state.
• Simplifies code maintenance and
enhances modularity.
Implementing
Encapsulatio
n in Java
Access Modifier
Example: Protected Modifier (1)
*Source: Stackoverflow
Example: Protected Modifier (2)
*Source: Stackoverflow
Example: Protected Modifier (3)
*Source: Stackoverflow
Example: Protected Modifier (4)
*Source: Stackoverflow
Advantages of
Encapsulation
Advantages
01 If you only provide a getter or setter method, you can make the class read-only or write-only.
You can control the data with it. You can implement logic inside the setter method if you want to set the id
02 value to be greater than 100. The setter method can get modified to exclude negative numbers from
storage.
03 In Java, private data members prevent other classes from accessing the data.
04 Encapsulate classes are easier to test, so they are better for unit testing.
In Java, encapsulated classes can get created quickly and easily using the standard IDEs that generate
05 getters and setters.
Real-world
Application of
Encapsulation
More efficient memory usage.
Banking Systems
01 Scenario: In a banking
system, customer accounts
need to be managed securely
to prevent unauthorized
access and manipulation of
sensitive data like account
balance, transaction history,
and personal information.
Banking Systems (2)
02 Application of Encapsulation: 03 Benefits:
• Data Hiding: Account details are kept
• Ensures that account
private within the Account class,
balances cannot be directly
preventing direct access to the account
balance or modification of the account
altered, protecting against
• Data Hiding: Patient records are • Improves data security and privacy by
medical history, test results, and information privacy regulations (like HIPAA
Do some modifications,
random students will be
chosen.
Additional input checker in methods
Additional input checker in methods (1)
Additional input checker in methods (2)
Visit Us
elearning.umn.ac.id