0% found this document useful (0 votes)
14 views

Encapsulation in Object Oriented Programming

Uploaded by

NEW ACC
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Encapsulation in Object Oriented Programming

Uploaded by

NEW ACC
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Encapsulation in

Object-Oriented
Programming
Muhammad Uzair

BSE-23S-036

4A - Software Design & Architecture


What is Encapsulation?
Data Hiding Abstraction
Protecting data within a class Simplifying complex tasks
from unauthorized access or with a user-friendly
modification, ensuring data interface.
integrity and security.
Advantages of
Encapsulation
1 Maintainability 2 Reusability
Easier to modify and Encapsulated classes can
update code without be reused in different
affecting other parts of parts of the program or in
the program. other projects.

3 Security 4 Modularity
Data hiding prevents Encapsulation breaks
unauthorized access, down complex systems
enhancing data integrity into manageable units,
and security. improving code
organization.
Implementing Encapsulation
in Code
1 Define a Class
Create a class with private attributes and public
methods to access or modify those attributes.

2 Declare Private Attributes


Use access modifiers to make attributes private,
preventing direct access from outside the class.

3 Create Public Methods


Implement methods that provide controlled access to
private attributes, ensuring data integrity.
Getters and Setters
Getter
A method that returns the value of a private attribute.

Setter
A method that sets the value of a private attribute.

Controlled Access
Getters and setters allow controlled access to private
data, enforcing data validation and integrity.
Encapsulation Best
Practices
Use Descriptive Names Choose meaningful names for
classes, attributes, and
methods to enhance
readability and
maintainability.
Follow Access Modifiers Use access modifiers
consistently to clearly define
the visibility and access
control for class members.

Implement Validation Incorporate validation logic


within setter methods to
ensure data integrity and
prevent invalid data
assignments.

You might also like