[go: up one dir, main page]

0% found this document useful (0 votes)
2 views24 pages

02 Encapsulation

The document covers the concept of Encapsulation in Object-Oriented Programming (OOP), emphasizing its importance in data protection and code maintenance. It outlines how to implement encapsulation in Java, including the use of access modifiers, and discusses its advantages through real-world applications in banking and health information systems. The document also encourages students to analyze and modify code related to encapsulation as part of their learning process.

Uploaded by

zenithalya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

02 Encapsulation

The document covers the concept of Encapsulation in Object-Oriented Programming (OOP), emphasizing its importance in data protection and code maintenance. It outlines how to implement encapsulation in Java, including the use of access modifiers, and discusses its advantages through real-world applications in banking and health information systems. The document also encourages students to analyze and modify code related to encapsulation as part of their learning process.

Uploaded by

zenithalya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Object Oriented

Programming
Week 02 – Encapsulation

LECTURES:
• SAMUEL ADY SANJAYA
• AGUS SULAIMAN
• BUDI SITORUS
• ENRICO SISWANTO
Objectives

01 Students able to understand the


concept of Encapsulation in OOP

02 Students able to understand how to


interpret and code Encapsulation in
Java code

03 Students able to analyze the use of


Encapsulation, the advantage and
disadvantage
Encapsulation
in OOP
Bundling of data (attributes) and
methods (functions) that operate on
the data into a single unit, or class,
and restricting access to some of
the object's components.
Concept
Definition:
• Encapsulation is a core concept in OOP
that combines data and the methods
that manipulate data into a single unit
called a class.
• It restricts direct access to an object's
attributes, promoting safer and more
reliable code.

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

holder's details without proper validation. fraudulent activities.


• Controlled Access: Public methods like • Simplifies maintenance and
deposit, withdraw, and getBalance provide updates to the banking
controlled ways to modify and access the
system, as changes in
account's balance. These methods ensure
account management are
that only valid operations are performed,
such as checking for sufficient funds
encapsulated within the

before allowing a withdrawal. Account class.


Health Information Systems

01 Scenario: Electronic health


records (EHR) systems store
sensitive patient information,
requiring strict control over
who can view or modify these
records.
Health Information Systems (2)

02 Application of Encapsulation: 03 Benefits:

• Data Hiding: Patient records are • Improves data security and privacy by

encapsulated in a PatientRecord preventing unauthorized access to

class, with private fields for sensitive health information.

sensitive information such as • Facilitates compliance with health

medical history, test results, and information privacy regulations (like HIPAA

personal identifiers. in the United States) by centralizing

• Controlled Access: Public methods access control logic within the

provide access to patient records PatientRecord class.

based on user roles. For example, a


method getMedicalHistory might
only be accessible to healthcare
providers who have the necessary
permissions.
Topic
Discusssion
Within the same group Analyze these code:
- the modifiers
- the methods

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)

modify the code to


add one additional
method named
transfer, that require
source account,
destination account,
and amount to
transfer!
Different Modifier Comparison
Different Modifier Comparison (2)
Thank You 

Visit Us

elearning.umn.ac.id

You might also like