Chap1 Encapsulation
Chap1 Encapsulation
CO2039
Chapter 1: Object Oriented Programming
Revision – Encapsulation
04 BEST PRACTICES
05 CONCLUSION
2
INTRODUCTION TO
01 ENCAPSULATION
3
What is Encapsulation?
When creating new data types (classes) the details of the actual data
and the way operations work is hidden from the other programmers
who will use those new data types.
4
Why is Encapsulation important?
By controlling how data and methods are
accessed or modified, encapsulation ensures:
● Data Integrity: Prevents unauthorized or
accidental changes to data.
● Security: Sensitive data is hidden from
unauthorized access.
● Ease of Use: Users of the class do not need
to understand its internal complexities.
● Code Maintenance: Internal implementation
can change without impacting external
usage.
5
KEY CONCEPTS OF
02 ENCAPSULATION
6
Encapsulation
7
Core principles of Encapsulation
Data Hiding: The internal state of an object is kept private and can only
be accessed through public methods (getters and setters).
Access Control: Access specifiers are used to control the visibility of class
members:
● private: members (attributes & methods) cannot be accessed (or
viewed) from outside the class ⇒ Default.
● protected: members cannot be accessed from outside the class, but
can be accessed in inherited classes. Learn more about Chap 2 -
Inheritance.
● public: members are accessible from outside the class.
8
Example
class BankAccount {
private:
double balance; string password;
public:
BankAccount(double initBalance, string pwd) : balance(initBalance), password(pwd) {}
Java Python
10
HOW
03 ENCAPSULATION
WORKS?
11
How Encapsulation works?
12
Constructor in Encapsulation
13
Constructor characteristics
14
Constructor
15
Setters & Getters
16
04 BEST PRACTICES
17
Exercise 1 - Multiple constructor
Create a Car class with the following private attributes:
● brand (string)
● model (string)
● year (int)
Add the following:
● A default constructor that sets default values.
● A parameterized constructor to initialize all attributes.
● Setters and getters for all attributes.
Write a main function to:
● Create an object using the default constructor and display its details.
● Create another object using the parameterized constructor and display its
details.
18
Exercise 2 - Constructor chaining
19
Exercise 3 (Bonus)
20
05 CONCLUSION
21
Conclusion
22
EXTENSION:
06 CLASS DIAGRAM
23
Class diagram
24
Hands-on exercise A class called Ball, which models
a bouncing ball, is designed as
shown in the following class
diagram.
It contains its radius, x and y
position. Each move-step
advances the x and y by delta-x
and delta-y, respectively. delta-x
and delta-y could be positive or
negative.
The reflectHorizontal() and
reflectVertical() methods could
be used to bounce the ball off
the walls. Write the Ball class.
Study the main() on how the
ball bounces.
https://www.programiz.com/onl
ine-compiler/5VrKcTuK0GRr6
25
Thank you for your
attention!
https://www.cse.hcmut.edu.vn