03-Abstraction & Encapsulation
03-Abstraction & Encapsulation
Outline
1 2
3 4
3 4
1
3/9/21
5 6
• Need to be simplified by ignoring all the unnecessary • What are the common properties of these entities? What
details are particular properties?
• All are Nokia phones
• Only “extract” related/involving, important information to
• Sliding, folding, …
the problem
• Phones for Businessman, Music, 3G
• QWERTY keyboard, Basic Type, No-keyboard type
• Color, Size, …
5 6
7 8
7 8
2
3/9/21
9 10
9 10
11 12
• organisms, mammals,
• organisms, mammals,
humans
dangerous mammals
11 12
3
3/9/21
13
13 14
15
Professor
Class representation in UML Relationship Between Classes and Objects
• A class is an abstract definition of an object
• It defines the structure and behavior of each object in
• Class is represented by a rectangle
Professor the class
with three parts: - name
- employeeID : UniqueId • It serves as a template for creating objects
- hireDate
• Class name - status • Classes are not collections of objects.
- discipline
- maxLoad
• Structure (Attributes)
+ submitFinalGrade()
+ acceptCourseOffering()
• Behavior (Operation) + setMaxLoad() Professor Torpie Professor
+ takeSabbatical()
+ teachClass()
15 16
4
3/9/21
Student
- name
- address Public Protected
Attributes
- studentID operations operations
- dateOfBirth
17 18
:Student
19 20
5
3/9/21
21 22
21 22
23 24
BankAccount
2.1. Encapsulation (2) 2.2. Class Building - owner: String
- balance: double
• Data/attributes and behaviors/methods are encapsulated
in a class à Encapsulation + debit(double): boolean
+credit(double)
• Attributes and methods are members of the class • Class name
• Specify what the abstraction is capturing
• Should be singular, short, and clear identify the concept
BankAccount • Data elements
• The pieces of data that an instance of the class holds
- owner: String
- balance: double • Operations/Messages
• List of messages that instances can receive
+ debit(double): boolean
+ credit(double) • Methods
• Implementations of the messages that each instance
can receive
23 24
6
3/9/21
25 26
message
:A
2.2. Class Building (2)
a1(5)
:B
• Class encapsulating members
+a1() • Attributes/Fields
+a2() behavior
/operation • Methods
class A {
- attr1;
String owner; Attribute declarations
double balance;
- attr2;
- attr3;
- attr4;
+a1(int i){
//tính tiền trong ví Method declarations
method //tính tiền trong tài khoản…
//trả về tổng số tiền
}
25 26
27 28
BankAccount
Class Building in Java a. Class declaration - owner: String
- balance: double
• Classes are grouped into a package • Declaration syntax:
package packagename;
• Package is composed of a set of classes that have + debit(double): boolean
access_modifier class ClassName{
some logic relation between them, // Class body +credit(double)
• Package is considered as a directory, a place to }
organize classes in order to locate them easily.
• Example: • access_modifier:
• public: Class can be accessed from anywhere, including outside its
• Some packages already available in Java: java.lang, package.
javax.swing, java.io… • private: Class can only be accessed from inside the class
• None (default): Class can be access from inside its package
• Packages can be manually defined by users
• Separated by “.”
=> Class declaration for BankAccount class?
• Convention for naming package
• Example: package oolt.hedspi;
27 28
7
3/9/21
29 30
29 30
31 32
Attribute Attribute
• Attributes have to be declared inside the class • Attribute can be initialized while declaring
• An object has its own copy of attributes • The default value will be used if not initialized.
• The values of an attribute of different objects are different.
Student
BankAccount
- name
- owner: String
- address
- balance: double
- studentID
- dateOfBirth
+ debit(double): boolean
Nguyễn Thu Hương
… + credit(double)
Nguyễn Hoàng Nam Hải Phòng…
Hà Nội…
31 32
8
3/9/21
33 34
boolean
33 34
35 36
35 36
9
3/9/21
37 38
39 40
39 40
10
3/9/21
41 42
41 42
43 44
43 44
11
3/9/21
45 46
Discussion
• Should we make all attributes private and
provide getter and setter methods for all
attributes of a class?
45 46
47 47 48 48
47 48
12
3/9/21
49 49 50 50
Hà Nội…
49 50
51 51 52 52
51 52
13
3/9/21
53 53 54 54
53 54
55 55 56 56
55 56
14
3/9/21
57 57 58 58
57 58
59 59 60 60
Example 1 Example 2
public class BankAccount{
public class BankAccount{ private String owner;
private String owner; private double balance;
private double balance;
public BankAccount(){
} owner = "noname";
public class Test{ }
public static void main(String args[]){ }
BankAccount acc1 = new BankAccount(); public class Test{
} public static void main(String args[]){
} BankAccount acc1 = new BankAccount();
}
à Default constructor provided by Java.
}
à Default constructor written by developers.
59 60
15
3/9/21
61 61
Example 3
public class BankAccount { Objects in C++ and Java
private String owner;
private double balance; • C++: objects in a class are created at the declaration:
public BankAccount(String name){ • Point p1;
setOwner(name); • Java: Declaration of an object creates only a reference
} that will refer to the real object when new operation is
public void setOwner(String o){
used:
owner = o;
• Box x;
}
• x = new Box();
} The constructor BankAccount() is undefined
public class Test{ • Objects are dynamically allocated in heap memory
public static void main(String args[]){
BankAccount account1 = new BankAccount(); //Error
BankAccount account2 = new BankAccount("Hoang");
}
}
61 62
64 64
63 64
16
3/9/21
65 65 66 66
65 66
67 68 68
67 68
17
3/9/21
69 69
69
18