Object oriented, Procedural Oridented, Encapsulation
Procedural Oridented ๐ Object oriented
- Procedural Oridented: ์์ ์ค์, how, ๋ฉ์๋ ๋ถ๋ฆฌ
- Object oriented: ๊ฐ์ฒด, what, ๊ฐ์ฒด์์ ๋ฉ์๋ ํฌํจ
โ example of procedural programming
example of Music Player
โ๏ธ music player class
1
2
3
4
public class MusicPlayerData {
int volume;
boolean isOn;
}
- โ๏ธ class with method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MusicPlayerMain {
public static void main(String[] args) {
MusicPlayerData data = new MusicPlayerData();
turnOn(data);
raiseVolume(data);
}
static void turnOn(MusicPlayerData data){
data.isOn = true;
System.out.println("music player is on");
}
static void raiseVolume(MusicPlayerData data){
data.volume++;
System.out.println("volume: " + data.volume);
}
- more focused on the order (turn on, then raise volume)
- ๐๐ป music player
field
andmethod
are in different classes - one in
music player class
and other inmain class
field
andmethod
of music player are very very relevent- can we find a way to put
field
andmethod
in one class?
โ change into OOP
- โ๏ธ music player class
- has both
field
andmethod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MusicPlayer {
int volume; //field
boolean isOn; //field
void turnOn(){ //method
isOn = true;
System.out.println("music player is on");
}
void raiseVolume(){ //method
volume++;
System.out.println("volume: " + volume);
}
}
- โ๏ธ main class
- only create instance, calls the method
1
2
3
4
5
6
7
8
public class MusicPlayerMain4 {
public static void main(String[] args) {
MusicPlayer player = new MusicPlayer();
player.turnOn();
player.raiseVolume();
}
}
more focused on how the music player works
- ๐๐ป
field
andmethod
are in one class, easier for the programmer to understand - ๐๐ป programming looks more like real life
- ๐๐ป easier to change code
- only has to change
music player class
,main class
does not change
- only has to change
โ Encapsulation
OOP
์์์ฒ๋ผfield
andmethod
๋ฅผ ํ ํด๋์ค์ ์บก์๋ก ๊ฐ์ธ๋ฏ์ด ๋ชจ์ ๊ฒ์์บก์ํ
๋ผ๊ณ ํ๋ค.์บก์ํ
:field
andmethod
๋ฅผ ๋ฌถ์ด์ ์ธ๋ถ์ ์ ๊ณตํ๋ ๊ฒ- limit access with access modifiers
This post is licensed under CC BY 4.0 by the author.