๐พ [CS] ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(Object-Oriented Programming, OOP)๋ ๋ฌด์์ผ๊น์?
- ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(Object-Oriented Programming, OOP)์ ํ๋ก๊ทธ๋จ์ ๊ฐ์ฒด(Object)๋ฅผ ์ค์ฌ์ผ๋ก ๊ตฌ์ฑํ๋ ํ๋ก๊ทธ๋๋ฐ ํจ๋ฌ๋ค์์ ๋๋ค.
- ๊ฐ์ฒด(Object)๋ ๋ฐ์ดํฐ์ ์ด ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์๋ฅผ ๋ฌถ์ ๊ฐ๋ ์ผ๋ก, ํ์ค ์ธ๊ณ์ ์ฌ๋ฌผ์ ํ๋ก๊ทธ๋จ ๋ด์์ ๋ชจ๋ฐฉํ์ฌ ์ค๊ณํ๋ ๋ฐฉ์์ ๋๋ค.
- ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(Object-Oriented Programming, OOP)์ ํต์ฌ ๊ฐ๋ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๐โโ๏ธ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์์์ ๊ฐ์ฒด(Object)๋ ๋ฌด์์ผ๊น์?
1๏ธโฃ ํด๋์ค์ ๊ฐ์ฒด.
๐ ํด๋์ค(Class).
- ๊ฐ์ฒด(Object)๋ฅผ ์์ฑํ๊ธฐ ์ํ ์ฒญ์ฌ์ง์ด๋ ํ์
๋๋ค.
- ํด๋์ค๋ ์์ฑ(๋ฐ์ดํฐ)๊ณผ ๋ฉ์๋(ํจ์)๋ฅผ ์ ์ํฉ๋๋ค.
๐ ๊ฐ์ฒด(Object).
- ํด๋์ค(Class)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ ์ค์ฒด๋ก, ํด๋์ค์ ์ ์๋ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ๊ฐ์ฒด๋ ํด๋์ค์ ์ธ์คํด์ค๋ผ๊ณ ๋ ๋ถ๋ฆฝ๋๋ค.
๐ ์์.
// ํด๋์ค ์ ์
class Car {
// ์์ฑ (ํ๋)
String model;
String color;
// ์์ฑ์
public Car(String model, String color) {
this.model = model;
this.color = color;
}
// ๋ฉ์๋
public void drive() {
System.out.println(model + "์ด(๊ฐ) ๋ฌ๋ฆฝ๋๋ค.");
}
}
// ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
public class Main {
public static void main(String[] args) {
// Car ํด๋์ค์ ์ธ์คํด์ค(๊ฐ์ฒด) ์์ฑ.
Car car1 = new Car("Volvo xc60", "Black");
car1.drive(); // ์ถ๋ ฅ: Volvo xc60์ด(๊ฐ) ๋ฌ๋ฆฝ๋๋ค.
}
}
- ์ ์์๋ Car๋ผ๋ ํด๋์ค๋ฅผ ์ ์ํ๊ณ , ๊ทธ ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก Volvo xc60์ด๋ผ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ
drive()
๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ณผ์ ์ ๋๋ค.
2๏ธโฃ ์บก์ํ(Encapsulation)
- ๊ฐ์ฒด ๋ด๋ถ์ ๋ฐ์ดํฐ(์์ฑ)์ ์ด ๋ฐ์ดํฐ๋ฅผ ์กฐ์ํ๋ ๋ฉ์๋๋ฅผ ํ๋๋ก ๋ฌถ๋ ๊ฒ์ ๋งํฉ๋๋ค.
- ์บก์ํ(Encapsulation)๋ฅผ ํตํด ๊ฐ์ฒด(Object) ์ธ๋ถ์์๋ ๋ด๋ถ ๊ตฌํ์ ์์ง ๋ชปํ๊ฒ ํ๊ณ , ์ ๊ณต๋ ๋ฉ์๋๋ฅผ ํตํด์๋ง ๋ฐ์ดํฐ๋ฅผ ์ ๊ทผํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๊ฒ ๋ง๋ญ๋๋ค.
- ์ด๋ฅผ ํตํด ๋ฐ์ดํฐ ๋ณดํธ์ ์ฝ๋์ ์์ง์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
๐ ์์
class Person {
// private ์์ฑ (์ธ๋ถ์์ ์ง์ ์ ๊ทผํ ์ ์์)
private String name;
private int age;
// ์์ฑ์
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// public ๋ฉ์๋๋ฅผ ํตํด ๊ฐ์ ์ ์ผ๋ก ์ ๊ทผ
public String getName() {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 0) { // ์ ํจ์ฑ ๊ฒ์ฌ
this.age = age;
}
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Kobe", 25);
System.out.println(p.getName()); // ์ถ๋ ฅ: Kobe
p.setAge(30);
System.out.println(p.getAge()); // ์ถ๋ ฅ: 30
}
}
- ์ด ์์์์๋ Person ํด๋์ค์์ ์บก์ํ(Encapsulation)๊ฐ ์ ์ฉ๋์ด,
name
๊ณผage
๊ฐ์ ์์ฑ์private
์ผ๋ก ์ ์ธ๋์ด ์ธ๋ถ์์ ์ง์ ์ ๊ทผํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ์กฐ์ํ๊ธฐ ์ํด์๋ ์ ๊ณต๋ ๋ฉ์๋(getName()
,setAge()
)๋ฅผ ํตํด ๊ฐ์ ์ ์ผ๋ก ์ ๊ทผํ๊ฒ ๋ฉ๋๋ค.
3๏ธโฃ ์์(Inheritance)
- ์์(Inheritance)์ ๊ธฐ์กด ํด๋์ค๋ฅผ ํ์ฅํ์ฌ ์๋ก์ด ํด๋์ค๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์
๋๋ค.
- ์์ ํด๋์ค๋ ๋ถ๋ชจ ํด๋์ค์ ์์ฑ๊ณผ ๋งค์๋๋ฅผ ๋ฌผ๋ ค๋ฐ์ ์ฌ์ฌ์ฉํ๋ฉฐ, ํ์ํ๋ฉด ์ถ๊ฐ๋ก ๊ธฐ๋ฅ์ ํ์ฅํ๊ฑฐ๋ ์์ ํ ์ ์์ต๋๋ค.
- ์์(Inheritance)์ ํตํด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ , ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
๐ ์์
// ๋ถ๋ชจ ํด๋์ค.
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void makeSound() {
System.out.println("๋๋ฌผ์ด ์๋ฆฌ๋ฅผ ๋
๋๋ค.")
}
}
// ์์ ํด๋์ค (์์)
class Dog extends Animal {
public Dog(String name) {
super(name); // ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถ
}
// ๋ถ๋ชจ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ
@Override
public void makeSound() {
System.out.println(name + "์ด(๊ฐ) ๋ฉ๋ฉ ์ง์ต๋๋ค.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("๋๋ฅด");
dog.makeSound(); // ์ถ๋ ฅ: ๋๋ฅด์ด(๊ฐ) ๋ฉ๋ฉ ์ง์ต๋๋ค.
}
}
- ์ ์์์์, Dog ํด๋์ค(Class)๋ Animal ํด๋์ค(Class)๋ฅผ ์์๋ฐ์ makeSound() ๋ฉ์๋๋ฅผ ์ฌ์ ์(Overriding, ์ค๋ฒ๋ผ์ด๋ฉ)ํ๊ณ , ์ด๋ฆ์ ์ถ๋ ฅํ๋๋ก ํ์ฅํ์ต๋๋ค.
- ์์(Inheritance)์ ํตํด ๋ถ๋ชจ ํด๋์ค์ ๊ธฐ๋ฅ์ ์ฌ์ฌ์ฉํ๋ฉด์๋ ํ์์ ๋ฐ๋ผ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์์ต๋๋ค.
4๏ธโฃ ๋คํ์ฑ(Polymorphism)
-
๋คํ์ฑ(Polymorphism)์ ๊ฐ์ ์ด๋ฆ์ ๋ฉ์๋๊ฐ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ๋์ํ ์ ์๊ฒ ํ๋ ๊ธฐ๋ฅ์
๋๋ค.
- ์์(Inheritance) ๊ด๊ณ์์ ๋ถ๋ชจ ํด๋์ค(Parents class, Super class)์ ๋ฉ์๋(Methods)๋ฅผ ์์ ํด๋์ค(Child class, Sub class)์์ ์ฌ์ ์(์ค๋ฒ๋ผ์ด๋ฉ, Overriding)ํ์ฌ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ๋์ํ๊ฒ ํ๊ฑฐ๋, ๊ฐ์ ์ด๋ฆ์ ๋ฉ์๋๊ฐ ์๋ก ๋ค๋ฅธ ๋งค๊ฐ๋ณ์(Paremeter)์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋์(์ค๋ฒ๋ก๋ฉ, Overloading)ํ ์ ์์ต๋๋ค.
- ๋คํ์ฑ(Polymorphism)์ ํตํด ์ฝ๋์ ์ ์ฐ์ฑ๊ณผ ํ์ฅ์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
๐ ์์.
class Animal {
public void makeSound() {
System.out.println("๋๋ฌผ์ด ์๋ฆฌ๋ฅผ ๋
๋๋ค.");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("๋ฉ๋ฉ");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("์ผ์น");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.makeSound(); // ์ถ๋ ฅ: ๋ฉ๋ฉ
animal2.makeSount(); // ์ถ๋ ฅ: ์ผ์น
}
}
- ์ด ์์๋ ๋คํ์ฑ(Polymorphism)์ ๋ณด์ฌ์ฃผ๋ ์ข์ ์์
๋๋ค.
-
Animal
ํ์ ์ ๋ณ์์ Dog์ Cat ๊ฐ์ฒด๋ฅผ ํ ๋นํ ์ ์์ผ๋ฉฐ,makeSound()
๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๊ฐ์ฒด์ ํ์ ์ ๋ง๊ฒ ๋ค๋ฅด๊ฒ ๋์ํฉ๋๋ค.- ์ด์ฒ๋ผ ๋ถ๋ชจ ํด๋์ค ํ์ ์ผ๋ก ๋ค์ํ ์์ ๊ฐ์ฒด๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
-
5๏ธโฃ ์ถ์ํ(Abstraction)
- ์ถ์ํ(Abstraction)์ ๋ณต์กํ ํ์ค์ ๊ฐ์ฒด์์ ํ์ํ ๋ถ๋ถ๋ง์ ๋ชจ๋ธ๋งํ์ฌ ๊ฐ์ฒด๋ก ํํํ๋ ๊ณผ์ ์
๋๋ค.
- ๋ถํ์ํ ์ธ๋ถ ์ฌํญ์ ์จ๊ธฐ๊ณ ์ค์ํ ๋ถ๋ถ๋ง ๋๋ฌ๋ด์ด ํจ์จ์ ์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐ ๋์์ ์ค๋๋ค.
- ์ถ์ํ(Abstraction)๋ ์ธํฐํ์ด์ค(Interface)๋ ์ถ์ ํด๋์ค๋ฅผ ํตํด ๊ตฌํ๋ฉ๋๋ค.
๐ ์์.
// ์ถ์ ํด๋์ค
abstract class Vehicle {
String model;
public Vehicle(String model) {
this.model = model;
}
// ์ถ์ ๋ฉ์๋ (๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ํ์ ํด๋์ค์์)
public abstract void move();
}
// ์์ ํด๋์ค (๊ตฌ์ฒด์ ์ธ ๊ตฌํ ์ ๊ณต)
class Car extends Vehicle {
public Car(String model) {
super(model);
}
@Override
public void move() {
System.out.println(model + "์ด(๊ฐ) ๋๋ก์์ ๋ฌ๋ฆฝ๋๋ค.");
}
}
class Airplane extends Vehicle {
public Airplane(String model) {
super(model);
}
@Override
public void move() {
System.out.println(model + "์ด(๊ฐ) ํ๋์ ๋ฉ๋๋ค.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car("Volvo xc60");
Vehicle airplane = new Airplane("Boeing 747");
car.move(); // ์ถ๋ ฅ: Volvo xc60์ด(๊ฐ) ๋๋ก์์ ๋ฌ๋ฆฝ๋๋ค.
airplane.move(); // ์ถ๋ ฅ: Boeing 747์ด(๊ฐ) ํ๋์ ๋ฉ๋๋ค.
}
}
- ์ ์์์์๋ ์ถ์ ํด๋์ค(Abstract class)๋ฅผ ํตํด์ ์ถ์ํ(Abstraction)๋ฅผ ๋ณด์ฌ์ค๋๋ค.
-
Vehicle ํด๋์ค๋ ์ถ์ ํด๋์ค(Abstract class)์ด๋ฉฐ, ์์ ํด๋์ค์ธ Car ์ Airplane์ด ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ์ ๊ณตํฉ๋๋ค.
- ์ถ์ํ(Abstraction)๋ฅผ ํตํด ๊ณตํต์ ์ธ ๋์์ ์ ์ํ๋ฉด์๋ ๊ฐ ๊ฐ์ฒด๊ฐ ๊ฐ๋ณ์ ์ธ ๋์์ ๊ตฌํํ ์ ์์ต๋๋ค.
-
Vehicle ํด๋์ค๋ ์ถ์ ํด๋์ค(Abstract class)์ด๋ฉฐ, ์์ ํด๋์ค์ธ Car ์ Airplane์ด ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ์ ๊ณตํฉ๋๋ค.
6๏ธโฃ ๊ฐ๋ฌด๋ฆฌ.
- ์ด๋ฌํ ๊ฐ๋ ์ ๋ฐํ์ผ๋ก ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(Object-Oriented Programming, OOP)์ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ , ์ ์ง๋ณด์์ ํ์ฅ์ฑ์ ๊ฐ์ ํ๋ฉฐ, ํ์ค ์ธ๊ณ์ ๋ฌธ์ ๋ฅผ ๋ ์ง๊ด์ ์ผ๋ก ํด๊ฒฐํ ์ ์๋๋ก ํฉ๋๋ค.