μλ°μ κ°μ²΄μ§ν₯νλ‘κ·Έλλ°μ μ΄ν΄ν λ μ€μν κ°λ μΌλ‘ κΌ½νλ κ²μ΄ λ€κ°μ§ μλ€.
1. ν΄λμ€
2. μμ
3. λ€νμ±
4. μΊ‘μν
μ΄μ€μμ ν΄λμ€μ μμμ μ§λ κ²μκΈλ€μ ν΅ν΄ λ€λ£¨μ΄λ΄€μΌλ
μ€λμ λ€νμ±κ³Ό μΊ‘μνμ λν΄ μ 리νλ μκ°μ κ°μ Έλ³΄μ.
λ¨Όμ , π λ€νμ±(polymorphism)μ΄λ?
- Polymorphism = Poly (λ§μ, μ¬λ¬ κ°) + Morphism (νν, ꡬ쑰)
μ¦, μ¬λ¬ κ°μ§ ννλ₯Ό κ°μ§ μ μλ€λ μλ―Έλ‘ μλ°μ λ€νμ± κ°λ
μμλ
- κ°μ λ©μλ(λλ μΈν°νμ΄μ€) κ° μλ‘ λ€λ₯Έ κ°μ²΄μμ λ€λ₯΄κ² λμν μ μλ κ²μ μλ―Ένλ€.
- κ°μ λΆλͺ¨ ν΄λμ€λ₯Ό κ°μ§ κ°μ²΄κ° μ¬λ¬ νν(λ€μν νμ
)λ‘ λμν μ μλ κ²
- μμ ν΄λμ€κ° λΆλͺ¨ ν΄λμ€μ νμ
μΌλ‘ μ°Έμ‘°λ μ μμ (Bicycle myBike = new MountainBike();)
- "λΆλͺ¨ νμ μΌλ‘ μμμ λ€λ£¨κ³ μ€λ²λΌμ΄λ©μ νμ©νλ κ²"
λ€νμ±(polymorphism) μμ
// λΆλͺ¨ ν΄λμ€
class Animal {
void makeSound() {
System.out.println("Some sound...");
}
}
// μμ ν΄λμ€ 1 - dog
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark! Bark!");
}
}
// μμ ν΄λμ€ 2 - cat
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow! Meow!");
}
}
// λ©μΈ λ©μλ
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
// κ°μ λ©μλλ₯Ό λκ°μ ν΄λμ€μμ λ€λ₯΄κ² ꡬννμλ€.
myDog.makeSound(); // μΆλ ₯: Bark! Bark!
myCat.makeSound(); // μΆλ ₯: Meow! Meow!
}
}
- Animal ν΄λμ€μ makeSound() λ©μλλ₯Ό μμ λ°μ Dog, Cat ν΄λμ€μμ κ°κ° μ€λ²λΌμ΄λ©(μ¬μ μ) ν¨.
- μ¬κΈ°μ μ€μν κ²μ, Dogκ³Ό Cat μ νμ μ΄ λΆλͺ¨ ν΄λμ€ νμ μ
- λμΌν makeSound() λ©μλλ₯Ό νΈμΆν΄λ κ°μ²΄ νμ μ λ°λΌ λ€λ₯Έ κ²°κ³Όκ° λμ¨λ€.
- λ€νμ±μ ν΅μ¬μ μ€λ²λΌμ΄λ©(Overriding) κ³Ό μΈν°νμ΄μ€(interface) νμ©
- λΆλͺ¨ νμ μΌλ‘ μμ κ°μ²΄λ₯Ό λ€λ£¨λ©΄μ μ€λ²λΌμ΄λ©λ λ©μλλ₯Ό μ€ννλ κ²μ΄ λ€νμ±μ ν΅μ¬!
π μΊ‘μν(encapsulation) λ?
Encapsulation = En-(μμ) + Capsule(μΊ‘μ) + -ation(λͺ μ¬ν μ λ―Έμ¬)
μ¦, μΊ‘μ μμ κ°λ , ν¬μ₯ν¨, κ°μΈμ 보νΈν¨μ΄λΌλ μλ―Έλ₯Ό κ°κ³ μμ
νλ‘κ·Έλλ°μμλ κ°μ²΄μ λ°μ΄ν°(νλ)λ₯Ό μΈλΆμμ μ§μ μ κ·Όνμ§ λͺ»νλλ‘ κ°μΈλ κ²μ μλ―Ένλ©°
νμν λ©μλλ₯Ό ν΅ν΄μλ§ λ°μ΄ν° λ³κ²½μ΄ κ°λ₯νλλ‘ μ ννλ κ²μ λ§νλ€.
-μλ°μμλ, κ°μ²΄μ νλλ₯Ό μ§μ μ κ·Όνμ§ λͺ»νλλ‘ private ν€μλλ₯Ό μΆκ°ν λ€ λ°μ΄ν°μ μμ νκ² μ κ·Όνλλ‘ getter/setter λ₯Ό μ 곡νκ±°λ, λ©μλλ₯Ό μ 곡νλ€.
- μΊ‘μνμ ν΅μ¬μ λ°μ΄ν°λ₯Ό 보νΈνκ³ , μμ ν μ κ·Ό λ°©μμ μ 곡νλ€.
μΊ‘μν(Encapsulation) μμ
class BankAccount {
private int balance = 0; // μ§μ μ κ·Ό λΆκ°λ₯ (private)
// balance κ°μ λ³κ²½νλ λ©μλ (setter)
public void deposit(int amount) {
if (amount > 0) {
balance += amount;
System.out.println(amount + "μμ΄ μ
κΈλμμ΅λλ€.");
} else {
System.out.println("μ¬λ°λ₯Έ κΈμ‘μ μ
λ ₯νμΈμ.");
}
}
// balance κ°μ μ½λ λ©μλ (getter)
public int getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
System.out.println("νμ¬ μμ‘: " + account.getBalance());
}
}
μ λ΄μ©μ μ§νΌν°νν λ¨Όμ λ¬Όμ΄λ΄μ κ°λ μ λ€μ νλ² λ¨Έλ¦¬μ λ£κ³
곡μλ¬Έμ μ½λ λ²λ¦μ λ€μ΄κΈ° μν΄ κ³΅μ λ¬Έμλ₯Ό μ°Ύμλ΄€λ€
λ€νμ±(polymorphism)
π곡μ λ¬Έμ λ΄μ© μ 리
λ€νμ±(Polymorphism)μ μλ¬Όνμμ νλμ μ’ μ΄λ κ°μ²΄κ° μ¬λ¬ λ€λ₯Έ ννλ₯Ό κ°μ§ μ μλ νΉμ±μ μλ―Ένλ€.
Java κ°μ μΈμ΄μμ λΆλͺ¨ ν΄λμ€μ κΈ°λ₯μ 곡μ νλ©΄μλ, κ° μμ ν΄λμ€κ° κ³ μ ν λμμ μ μν μ μλλ‘ νλ μ리
+ μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μ€λ²λΌμ΄λ© ν΄μΌν¨.
μμ 1 - λΆλͺ¨ ν΄λμ€ (Bicycle)
public class Bicycle {
private int gear;
private int cadence;
private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
this.cadence = startCadence;
this.speed = startSpeed;
this.gear = startGear;
}
public void printDescription() {
System.out.println("\nBike is in gear " + this.gear
+ " with a cadence of " + this.cadence +
" and travelling at a speed of " + this.speed + ".");
}
}
μμ 2 - μμ ν΄λμ€ (MountainBike)
public class MountainBike extends Bicycle {
private String suspension;
public MountainBike(int startCadence, int startSpeed, int startGear, String suspensionType) {
super(startCadence, startSpeed, startGear);
this.setSuspension(suspensionType);
}
public String getSuspension(){
return this.suspension;
}
public void setSuspension(String suspensionType) {
this.suspension = suspensionType;
}
@Override
public void printDescription() {
super.printDescription(); // λΆλͺ¨ ν΄λμ€μ λ©μλ νΈμΆ
System.out.println("The MountainBike has a " + getSuspension() + " suspension.");
}
}
β MountainBike ν΄λμ€ μμ±μμ this.setSuspension(suspensionType); λ₯Ό μΆκ°ν΄μ ν΄λμ€κ° μμ±λ λ μ¬μ©μκ° μ λ ₯ν κ°μ΄ private λ³μμΈ suspension μ λ°λ‘ μ μ©λλλ‘ ν¨.
β μ€λ²λΌμ΄λ© λ΄μ©: λΆλͺ¨ ν΄λμ€μ printDescription()μ MountainBikeμ printDescription() μμμ 'νΈμΆ', μ¦ μμ ν΄λμ€μ printDescription μμμ λΆλͺ¨ ν΄λμ€μ printDescription λ₯Ό 'μ€ν'
β μ€λ²λΌμ΄λ© λͺ©μ : κΈ°λ³Έ μμ κ±°(Bicycle)μ λμΌν κΈ°λ₯μ κ°μ§λ©΄μλ, "MountainBikeμλ μ΄λ€ μμ€νμ μ΄ μλμ§"λ₯Ό μΆκ°λ‘ μΆλ ₯νλλ‘ printDescription()μ μ€λ²λΌμ΄λ©
π‘μμλ κ²μ !!! λΆλͺ¨ ν΄λμ€μ λ©μλ μ΄λ¦, 맀κ°λ³μ, λ°ν νμ μ΄ κ°μ → μ€λ²λΌμ΄λ© μ±λ¦½λλ€ !!!!
μμ 3 - μμ ν΄λμ€ (RoadBike)
public class RoadBike extends Bicycle {
private int tireWidth; // νμ΄μ΄ λλΉ (mm λ¨μ)
public RoadBike(int startCadence, int startSpeed, int startGear, int newTireWidth) {
super(startCadence, startSpeed, startGear);
this.setTireWidth(newTireWidth);
}
public int getTireWidth() {
return this.tireWidth;
}
public void setTireWidth(int newTireWidth) {
this.tireWidth = newTireWidth;
}
@Override
public void printDescription() { // μ€λ²λΌμ΄λ©λ λ©μλ
super.printDescription(); // λΆλͺ¨μ printDescription() μ€ν
System.out.println("The RoadBike has " + getTireWidth() + " MM tires.");
}
}
β μ λ΄μ©κ³Ό λμΌ !!
β tireWidrh νμ΄μ΄ λμ΄λ₯Ό λνλ΄λ λ³μλ₯Ό μΆκ°μ μΌλ‘ μμ±ν΄ printDescription λ©μλλ₯Ό μ€λ²λΌμ΄λ©ν λ€ μΆλ ₯νλ€. !
μ΄μ μ μν ν΄λμ€λ€μ μΆλ ₯ν΄λ³΄μ !!
μμ 4 - λ©μΈν΄λμ€
public class TestBikes {
public static void main(String[] args) {
Bicycle bike01, bike02, bike03;
bike01 = new Bicycle(20, 10, 1); // μΌλ° μμ κ±° κ°μ²΄
bike02 = new MountainBike(20, 10, 5, "Dual"); // μ°μ
μμ κ±° κ°μ²΄
bike03 = new RoadBike(40, 20, 8, 23); // λλ‘μ© μμ κ±° κ°μ²΄
bike01.printDescription();
bike02.printDescription();
bike03.printDescription();
}
}
μΆλ ₯ κ²°κ³Ό
Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10.
Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10.
The MountainBike has a Dual suspension.
Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20.
The RoadBike has 23 MM tires.
β λΆλͺ¨ ν΄λμ€(Bicycle) νμ μ λ³μμ μ¬λ¬ μμ ν΄λμ€(MountainBike, RoadBike)μ κ°μ²΄λ₯Ό ν λΉν μ μλ€.
β κ°κ° κ°μ²΄ νμ μ λ§λ printDescription λ©μλκ° μ€νλλ€.
JVMμ μ€μ κ°μ²΄μ νμ μ κΈ°μ€μΌλ‘ λ©μλλ₯Ό νΈμΆνλ€
- λ³μμ νμ μ΄ Bicycleμ΄μ§λ§, μ€μ κ°μ²΄κ° MountainBike λλ RoadBikeλΌλ©΄ ν΄λΉ ν΄λμ€μ μ€λ²λΌμ΄λ©λ λ©μλκ° μ€νλλ€.
- μ΄λ° λμμ "κ°μ λ©μλ νΈμΆ(Virtual Method Invocation)"μ΄λΌκ³ νλ€.
- μ¦, JVMμ λ³μμ μ μΈλ νμ (Bicycle)μ΄ μλλΌ, μ€μ κ°μ²΄μ νμ (MountainBike, RoadBike)μ κΈ°μ€μΌλ‘ λ©μλλ₯Ό μ€ννλ€.
λ€μμ μΆκ°ν κ²
μ μΊμ€ν (Upcasting)κ³Ό λ€μ΄μΊμ€ν (Downcasting) κ°λ
- Bicycle myBike = new MountainBike(); (μ μΊμ€ν )
- MountainBike realBike = (MountainBike) myBike; (λ€μ΄μΊμ€ν )
μ€λ²λΌμ΄λ©κ³Ό μ€λ²λ‘λ© μ°¨μ΄μ
- μ€λ²λΌμ΄λ©(Overriding) ≠ μ€λ²λ‘λ©(Overloading)
- μ€λ²λ‘λ©μ κ°μ μ΄λ¦μ΄μ§λ§ 맀κ°λ³μκ° λ€λ¦, μ€λ²λΌμ΄λ©μ λΆλͺ¨ λ©μλλ₯Ό μ¬μ μνλ κ²
μΊ‘μν(Encapsulation)
π곡μ λ¬Έμ λ΄μ© μ 리
μΊ‘μν(Encapsulation)λ?
λ°μ΄ν°λ₯Ό 보νΈνκ³ , μ§μ μ κ·Όμ μ°¨λ¨νμ¬ μλͺ»λ λ°μ΄ν° λ³κ²½μ λ°©μ§νλ κΈ°λ²
private λ©€λ² λ³μ + public λ©μλ(getter, setter)λ₯Ό ν΅ν΄ μ κ·Ό
μ§μ λ³μ κ°μ μμ νμ§ λͺ»νκ² νκ³ , λ©μλλ₯Ό ν΅ν΄μλ§ λ³κ²½ κ°λ₯νλλ‘ μ ν
λ¨Όμ ,
μλ°μμ λ³μλ₯Ό μ μΈνλ λ°©λ²μλ μ¬λ¬ κ°μ§κ° μλ€!
λ€μμ λ³μμ μ’ λ₯μ΄λ€
1οΈβ£ λ©€λ² λ³μ (fields) → ν΄λμ€ λ΄λΆμμ μ μΈλ λ³μ
2οΈβ£ μ§μ λ³μ (local variables) → λ©μλλ μ½λ λΈλ‘ μμμ μ μΈλ λ³μ
3οΈβ£ 맀κ°λ³μ (parameters) → λ©μλ μ μΈμμ μ λ¬λ°λ λ³μ
μλ°μμλ μ κ·Ό μ νμ(public, private λ±)λ₯Ό μ¬μ©νμ¬ ν΄λμ€ μΈλΆμμ νΉμ λ³μ(νλ)λ λ©μλμ μ κ·Όν μ μλ λ²μλ₯Ό μ μ΄ν μ μλ€.
μΌλ°μ μΌλ‘ μΊ‘μν(Encapsulation)μ μμΉμ μ§ν€κΈ° μν΄ λ©€λ² λ³μλ privateμΌλ‘ μ μΈνκ³ , public λ©μλλ₯Ό ν΅ν΄ μ κ·Όνλ κ²μ΄ μΌλ°μ μ΄λ€.
μμ 1 - private μ κ·Ό μ νμ μ¬μ©νμ¬ ν΄λμ€ λ©€λ² λ³μ μ μΈ
public class Bicycle {
private int cadence;
private int gear;
private int speed;
}
β
λ©€λ² λ³μλ₯Ό privateμΌλ‘ μ€μ νλ©΄ μΈλΆ ν΄λμ€μμ μ§μ μ κ·Όν μ μμ
β
νμ§λ§ private λ³μμ μ κ·ΌνκΈ° μν getterμ setter λ©μλλ₯Ό μΆκ°νλ©΄ κ°μ μ‘°μν μ μμ
getter & setter μ¬μ© μ΄μ
- μ§μ λ³μ μ κ·Όμ λ§κ³ , μμ ν λ°©λ²μΌλ‘ κ°μ μ½κ±°λ λ³κ²½νκΈ° μν΄ μ¬μ©
- getλ³μλͺ (), setλ³μλͺ (κ°) νμμΌλ‘ μ¬μ©
μμ 2- μΊ‘μνλ₯Ό μ μ©ν Bicycle ν΄λμ€
public class Bicycle {
private int cadence;
private int gear;
private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// Getter & Setter λ©μλ (μΊ‘μν)
public int getCadence() {
return cadence;
}
public void setCadence(int newValue) {
cadence = newValue;
}
public int getGear() {
return gear;
}
public void setGear(int newValue) {
gear = newValue;
}
public int getSpeed() {
return speed;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
+ λ³μ νμ (μλ£ν)
- μλ°μ λͺ¨λ λ³μλ μλ£ν(type)μ κ°μ ΈμΌ νλ€.
- κΈ°λ³Έ νμ (Primitive Type): int, float, boolean λ±
- μ°Έμ‘° νμ (Reference Type): String, λ°°μ΄(Array), κ°μ²΄(Object) λ±
+ λ€μ΄λ° κ·μΉ
β
λͺ¨λ λ³μλͺ
, νλλͺ
, λ©μλλͺ
μ μλ°μ λ€μ΄λ° κ·μΉμ λ°λΌμΌ νλ€.
β
ν΄λμ€λͺ
: PascalCase (λλ¬Έμλ‘ μμ, λ¨μ΄ μ°κ²° μ λλ¬Έμλ‘ κ΅¬λΆ)
β
λ³μλͺ
, λ©μλλͺ
: camelCase (μλ¬Έμλ‘ μμ, λ¨μ΄ μ°κ²° μ λλ¬Έμλ‘ κ΅¬λΆ)
λ€μμ μΆκ°ν κ²
- "μ μΊ‘μνλ₯Ό ν΄μΌ νλκ°?"μ λν μ€μ©μ μΈ μμ
(ex. setSpeed() λ©μλμ κ° κ²μ¦(Validation) λ‘μ§μ μΆκ°ν΄μ, μλͺ»λ κ°μ΄ λ€μ΄κ°λ κ±Έ λ§λ μμ )
Oracle Java νν λ¦¬μΌ λ¬Έμ μ’ν
1. λ€νμ±(Polymorphism)
https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Dev.java for updated tutorials taking advantag
docs.oracle.com
2. μΊ‘μν(Encapsulation)
https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Dev.java for updated tutorials taking advantag
docs.oracle.com
(곡μ λ¬Έμμ μλ λ΄μ©μ μλ λ΄μ©μ΄ λ°©λνκ³ λ§λ€λ³΄λ μ΄κ² λ€κ° μλ κ²μ΄λ€? )
Oracle Java νν 리μΌμμλ λ€νμ±(Polymorphism)κ³Ό μΊ‘μν(Encapsulation) κ° λ€λ₯Έ μΉ΄ν κ³ λ¦¬λ‘ λΆλ₯λμ΄ μλ€.
λ λ€ κ°μ²΄μ§ν₯νλ‘κ·Έλλ°(OOP) μ μ£Όμ κ°λ μ΄μ§λ§
μμμ λ³Έ κ²μ²λΌ λ€νμ±μ μμ(Inheritance)κ³Ό μΈν°νμ΄μ€(Interfaces) κ°λ κ³Ό λ°μ νκ² μ°κ²°λμ΄ μκ³
μΊ‘μν(Encapsulation)λ κ°μ²΄ λ΄λΆ λ°μ΄ν°λ₯Ό 보νΈνλ κ°λ μ΄λ―λ‘ ν΄λμ€μ κ°μ²΄ κ°λ μ μ€λͺ νλ λΆλΆμμ λ€λ€μ§λ€.