OOP ๊ด€๋ จ ์งˆ๋ฌธ์€ IT ํšŒ์‚ฌ์˜ Java ๊ฐœ๋ฐœ์ž ์œ„์น˜์— ๋Œ€ํ•œ ๊ธฐ์ˆ  ์ธํ„ฐ๋ทฐ์˜ ํ•„์ˆ˜์ ์ธ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ด ๊ธฐ์‚ฌ์—์„œ ์šฐ๋ฆฌ๋Š” OOP์˜ ํ•œ ๊ฐ€์ง€ ์›์น™์ธ ๋‹คํ˜•์„ฑ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ธํ„ฐ๋ทฐ ์ค‘์— ์ž์ฃผ ๋ฌป๋Š” ์ธก๋ฉด์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ๋ช…ํ™•์„ฑ์„ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Java์˜ ๋‹คํ˜•์„ฑ์ด๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋‹คํ˜•์„ฑ์€ ๊ฐ์ฒด์˜ ํŠน์ • ์œ ํ˜•์— ๋Œ€ํ•œ ์ •๋ณด ์—†์ด ๋™์ผํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋‹คํ˜•์„ฑ์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ์งˆ๋ฌธ์— ๋Œ€๋‹ตํ•˜๋Š” ๊ฒฝ์šฐ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋ฅผ ์„ค๋ช…ํ•˜๋ผ๋Š” ์š”์ฒญ์„ ๋ฐ›์„ ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค. ๋งŽ์€ ์ถ”๊ฐ€ ์งˆ๋ฌธ์„ ์œ ๋ฐœํ•˜์ง€ ์•Š๊ณ  ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋ฉด์ ‘๊ด€์„ ์œ„ํ•ด ๋ชจ๋“  ๊ฒƒ์„ ๋ฐฐ์น˜ํ•˜์‹ญ์‹œ์˜ค. ์ธํ„ฐ๋ทฐ ์‹œ๊ฐ„: Java์˜ ๋‹คํ˜•์„ฑ - 1OOP ์ ‘๊ทผ ๋ฐฉ์‹์—๋Š” ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๊ฐœ์ฒด ๊ฐ„์˜ ์ƒํ˜ธ ์ž‘์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ Java ํ”„๋กœ๊ทธ๋žจ์„ ๋นŒ๋“œํ•˜๋Š” ์ž‘์—…์ด ํฌํ•จ๋œ๋‹ค๋Š” ์‚ฌ์‹ค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ด์ „์— ์ž‘์„ฑ๋œ ์ฒญ์‚ฌ์ง„(ํ…œํ”Œ๋ฆฟ)์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํด๋ž˜์Šค์—๋Š” ํ•ญ์ƒ ํŠน์ • ์œ ํ˜•์ด ์žˆ์œผ๋ฉฐ ์ข‹์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ชฉ์ ์„ ์ œ์•ˆํ•˜๋Š” ์ด๋ฆ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ Java๋Š” ๊ฐ•๋ ฅํ•œ ํ˜•์‹์ด๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ํ•ญ์ƒ ๊ฐœ์ฒด ํ˜•์‹์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์—„๊ฒฉํ•œ ํƒ€์ดํ•‘์ด ์ฝ”๋“œ ๋ณด์•ˆ๊ณผ ์•ˆ์ •์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ณ  ์ปดํŒŒ์ผ ์‹œ์—๋„ ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š” ์œ ํ˜•(์˜ˆ: ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋‚˜๋ˆ„๊ธฐ ์‹œ๋„)์œผ๋กœ ์ธํ•œ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” "์•Œ๊ณ " ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ ์–ธ๋œ ์œ ํ˜• โ€“ JDK์˜ ํด๋ž˜์Šค์ด๊ฑฐ๋‚˜ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ๋งŒ๋“  ํด๋ž˜์Šค์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉด์ ‘๊ด€์—๊ฒŒ ์šฐ๋ฆฌ ์ฝ”๋“œ๋Š” ์„ ์–ธ์— ํ‘œ์‹œ๋œ ์œ ํ˜•์˜ ๊ฐ์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ทธ ์ž์†๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ์ง€์ ํ•˜์‹ญ์‹œ์˜ค.์ด๊ฒƒ์€ ์ค‘์š”ํ•œ ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์œ ํ˜•์„ ๋‹จ์ผ ์œ ํ˜•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ด๋Ÿฌํ•œ ์œ ํ˜•์ด ๊ธฐ๋ณธ ์œ ํ˜•์—์„œ ํŒŒ์ƒ๋œ ๊ฒฝ์šฐ). ์ด๋Š” ๋˜ํ•œ ์œ ํ˜•์ด ์Šˆํผํด๋ž˜์Šค์ธ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ฉด ํ•ด๋‹น ๋ณ€์ˆ˜์— ํ•˜์œ„ ํ•ญ๋ชฉ ์ค‘ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฉด์ ‘๊ด€์ด ์ข‹์•„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ํด๋ž˜์Šค(๊ธฐ๋ณธ ํด๋ž˜์Šค)์—์„œ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ถ€ ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ทธ ์ค‘ ๋ช‡ ๊ฐœ๊ฐ€ ์ƒ์†ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํด๋ž˜์Šค:

public class Dancer {
    private String name;
    private int age;

    public Dancer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void dance() {
        System.out.println(toString() + " I dance like everyone else.");
    }

    @Override
    public String toString() {
        Return "I'm " + name + ". I'm " + age + " years old.";
    }
}
ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

public class ElectricBoogieDancer extends Dancer {
    public ElectricBoogieDancer(String name, int age) {
        super(name, age);
    }
// Override the method of the base class
    @Override
    public void dance() {
        System.out.println(toString () + " I dance the electric boogie!");
    }
}

public class Breakdancer extends Dancer {

    public Breakdancer(String name, int age) {
        super(name, age);
    }
// Override the method of the base class
    @Override
    public void dance() {
        System.out.println(toString() + " I breakdance!");
    }
}
๋‹คํ˜•์„ฑ์˜ ์˜ˆ์™€ ์ด๋Ÿฌํ•œ ๊ฐœ์ฒด๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•:

public class Main {

    public static void main(String[] args) {
        Dancer dancer = new Dancer("Fred", 18);

        Dancer breakdancer = new Breakdancer("Jay", 19); // Widening conversion to the base type 
        Dancer electricBoogieDancer = new ElectricBoogieDancer("Marcia", 20); // Widening conversion to the base type

        List<dancer> disco = Arrays.asList(dancer, breakdancer, electricBoogieDancer);
        for (Dancer d : disco) {
            d.dance(); // Call the polymorphic method
        }
    }
}
์ฃผ์š” ๋ฐฉ๋ฒ• ์—์„œ ๋‹ค์Œ ํ–‰์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

Dancer breakdancer = new Breakdancer("Jay", 19);
Dancer electricBoogieDancer = new ElectricBoogieDancer("Marcia", 20);
์ˆ˜ํผํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ํ•˜์œ„ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค์ธ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ํ• ๋‹น ์—ฐ์‚ฐ์ž์˜ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์— ์„ ์–ธ๋œ ์œ ํ˜•์˜ ๋ถˆ์ผ์น˜๋กœ ์ธํ•ด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋’ค์ง‘ํžˆ์ง€ ์•Š๋Š” ์ด์œ ๋ฅผ ๋ฌผ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ Java๋Š” ๊ฐ•๋ ฅํ•œ ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ํ™•์žฅ ์œ ํ˜• ๋ณ€ํ™˜์ด ์—ฌ๊ธฐ์„œ ์ž‘๋™ํ•œ๋‹ค๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์ฐธ์กฐ์ฒ˜๋Ÿผ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฝ”๋“œ์—์„œ ์ด๋Ÿฌํ•œ ๊ตฌ์„ฑ์„ ๋งŒ๋‚˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ž๋™์œผ๋กœ ์•”์‹œ์ ์œผ๋กœ ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ƒ˜ํ”Œ ์ฝ”๋“œ๋Š” ํ• ๋‹น ์—ฐ์‚ฐ์ž( Dancer ) ์˜ ์™ผ์ชฝ์— ์„ ์–ธ๋œ ์œ ํ˜•์ด ์˜ค๋ฅธ์ชฝ ( Breakdancer , ElectricBoogieDancer ) ์— ์„ ์–ธ๋œ ์—ฌ๋Ÿฌ ํ˜•์‹(์œ ํ˜•)์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.). ๊ฐ ์–‘์‹์€ ์ˆ˜ํผํด๋ž˜์Šค( ๋Œ„์Šค ๋ฉ”์„œ๋“œ) ์— ์ •์˜๋œ ์ผ๋ฐ˜ ๊ธฐ๋Šฅ๊ณผ ๊ด€๋ จํ•˜์—ฌ ๊ณ ์œ ํ•œ ๋™์ž‘์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ์ฆ‰, ์ƒ์œ„ ํด๋ž˜์Šค์—์„œ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ๋Š” ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์— ์šฐ๋ฆฌ๋Š” ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋Š” ์ •ํ™•ํžˆ ์—ฌ๋Ÿฌ ํผ(๋™์ž‘)์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ฉ”์ธ ๋ฉ”์„œ๋“œ์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์ถœ๋ ฅ: I'm Fred. ์ €๋Š” 18 ์‚ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ฒ˜๋Ÿผ ์ถค์„ ์ถฅ๋‹ˆ๋‹ค. ์ €๋Š” ์ œ์ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” 19 ์‚ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋ธŒ๋ ˆ์ดํฌ ๋Œ„์Šค๋ฅผ ์ถ˜๋‹ค! ์ €๋Š” ๋งˆ๋ฅด์‹œ์•„์ž…๋‹ˆ๋‹ค. ๋‚ด ๋‚˜์ด๋Š” 20 ์‚ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ์ผ๋ ‰ํŠธ๋ฆญ ๋ถ€๊ธฐ๋ฅผ ์ถ˜๋‹ค! ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค๋ฅธ ๋™์ž‘์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด,ElectricBoogieDancer ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ์˜ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. I'm Fred. ์ €๋Š” 18 ์‚ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ฒ˜๋Ÿผ ์ถค์„ ์ถฅ๋‹ˆ๋‹ค. ์ €๋Š” ์ œ์ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” 19 ์‚ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ฒ˜๋Ÿผ ์ถค์„ ์ถฅ๋‹ˆ๋‹ค. ์ €๋Š” ๋งˆ๋ฅด์‹œ์•„์ž…๋‹ˆ๋‹ค. ๋‚ด ๋‚˜์ด๋Š” 20 ์‚ด์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ฒ˜๋Ÿผ ์ถค์„ ์ถฅ๋‹ˆ๋‹ค. ์ด๋Š” Breakdancer ๋ฐ ElectricBoogieDancer ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค . ๋‹คํ˜•์„ฑ์˜ ์›๋ฆฌ๋Š” ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋””์— ๋ช…์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๊นŒ? ํŠน์ • ์œ ํ˜•์— ๋Œ€ํ•œ ์ง€์‹ ์—†์ด ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ์ฒด๋Š” ์–ด๋””์— ์žˆ์Šต๋‹ˆ๊นŒ? ์ด ์˜ˆ์ œ์—์„œ๋Š” Dancer d ๊ฐœ์ฒด์—์„œ dance() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค . Java์—์„œ ๋‹คํ˜•์„ฑ์ด๋ž€ ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐ์ฒด๊ฐ€ ๊ฐ์ฒด์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Breakdancer ๋˜๋Š” ElectricBoogieDancer . ์ค‘์š”ํ•œ ๊ฒƒ์€ Dancer ํด๋ž˜์Šค ์˜ ํ›„์†์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค . ๊ทธ๋ฆฌ๊ณ  ์ž์†์„ ์–ธ๊ธ‰ํ•˜๋Š” ๊ฒฝ์šฐ Java์˜ ์ƒ์†์€ extends ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ตฌํ˜„. ์ด์ œ Java๊ฐ€ ๋‹ค์ค‘ ์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์„ ์–ธ๊ธ‰ํ•  ๋•Œ์ž…๋‹ˆ๋‹ค. ๊ฐ ์œ ํ˜•์€ ํ•˜๋‚˜์˜ ๋ถ€๋ชจ(์Šˆํผํด๋ž˜์Šค)์™€ ๋ฌด์ œํ•œ์˜ ์ž์†(์„œ๋ธŒํด๋ž˜์Šค)์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ ์ง‘ํ•ฉ์„ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋ž˜์Šค(์ƒ์†)์™€ ๋น„๊ตํ•  ๋•Œ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ƒ์œ„ ํด๋ž˜์Šค์™€ ๋œ ๊ฒฐํ•ฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋“ค์€ ๋งค์šฐ ๋„๋ฆฌ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Java์—์„œ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ฐธ์กฐ ์œ ํ˜•์ด๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋žจ์€ ์ธํ„ฐํŽ˜์ด์Šค ์œ ํ˜•์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณผ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค ๋งŒ๋“ค๊ธฐ:

public interface CanSwim {
    void swim();
}
๋ช…ํ™•์„ฑ์„ ์œ„ํ•ด ๊ด€๋ จ ์—†๋Š” ๋‹ค์–‘ํ•œ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

public class Human implements CanSwim {
    private String name;
    private int age;

    public Human(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public void swim() {
        System.out.println(toString()+" I swim with an inflated tube.");
    }

    @Override
    public String toString() {
        return "I'm " + name + ". I'm " + age + " years old.";
    }

}
 
public class Fish implements CanSwim {
    private String name;

    public Fish(String name) {
        this.name = name;
    }

    @Override
    public void swim() {
        System.out.println("I'm a fish. My name is " + name + ". I swim by moving my fins.");

    }

public class UBoat implements CanSwim {

    private int speed;

    public UBoat(int speed) {
        this.speed = speed;
    }

    @Override
    public void swim() {
        System.out.println("I'm a submarine that swims through the water by rotating screw propellers. My speed is " + speed + " knots.");
    }
}
์ฃผ์š” ๋ฐฉ๋ฒ•:

public class Main {

    public static void main(String[] args) {
        CanSwim human = new Human("John", 6);
        CanSwim fish = new Fish("Whale");
        CanSwim boat = new UBoat(25);

        List<swim> swimmers = Arrays.asList(human, fish, boat);
        for (Swim s : swimmers) {
            s.swim();
        }
    }
}
์ธํ„ฐํŽ˜์ด์Šค์— ์ •์˜๋œ ๋‹คํ˜•์„ฑ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋Š” ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์œ ํ˜•์˜ ๋™์ž‘ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ์ด๋“ค์€ swim ๋ฉ”์„œ๋“œ ์— ์˜ํ•ด ํ‘œ์‹œ๋˜๋Š” ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค . ์šฐ๋ฆฌ์˜ ์˜ˆ์ œ๋ฅผ ๊ณต๋ถ€ํ•œ ํ›„ ๋ฉด์ ‘๊ด€์€ ์™œ ์ด ์ฝ”๋“œ๋ฅผ ๋ฉ”์ธ ๋ฉ”์„œ๋“œ ์—์„œ ์‹คํ–‰ํ•˜๋Š”์ง€ ๋ฌผ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

for (Swim s : swimmers) {
            s.swim();        
}
ํ•˜์œ„ ํด๋ž˜์Šค์— ์ •์˜๋œ ์žฌ์ •์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋„๋ก ํ•ฉ๋‹ˆ๊นŒ? ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ๋ฉ”์†Œ๋“œ์˜ ์›ํ•˜๋Š” ๊ตฌํ˜„์€ ์–ด๋–ป๊ฒŒ ์„ ํƒ๋ฉ๋‹ˆ๊นŒ? ์ด๋Ÿฌํ•œ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋ ค๋ฉด ํ›„๊ธฐ(๋™์ ) ๋ฐ”์ธ๋”ฉ์„ ์„ค๋ช…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”์ธ๋”ฉ์ด๋ž€ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๊ณผ ํ•ด๋‹น ํŠน์ • ํด๋ž˜์Šค ๊ตฌํ˜„ ๊ฐ„์˜ ๋งคํ•‘์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ณธ์งˆ์ ์œผ๋กœ ์ฝ”๋“œ๋Š” ํด๋ž˜์Šค์— ์ •์˜๋œ ์„ธ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ ์ค‘ ์‹คํ–‰๋  ๋ฉ”์„œ๋“œ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. Java๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ›„๊ธฐ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ดˆ๊ธฐ ๋ฐ”์ธ๋”ฉ์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ์ปดํŒŒ์ผ ํƒ€์ž„์ด ์•„๋‹ˆ๋ผ ๋Ÿฐํƒ€์ž„์— ๋ฐ”์ธ๋”ฉ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ด ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•  ๋•Œ

for (Swim s : swimmers) {
            s.swim();        
}
์–ด๋–ค ํด๋ž˜์Šค( Human , Fish ๋˜๋Š” Uboat )์— ์ˆ˜์˜ ํ•  ๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋™์  ๋ฐ”์ธ๋”ฉ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ๋•๋ถ„์— ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ๋งŒ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค(๋Ÿฐํƒ€์ž„์— ๊ฐœ์ฒด ์œ ํ˜•์„ ํ™•์ธํ•˜๊ณ  ์ด ์œ ํ˜•์— ๋Œ€ํ•œ ์˜ฌ๋ฐ”๋ฅธ ๊ตฌํ˜„ ์„ ํƒ). ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜๋Š”์ง€ ๋ฌป๋Š”๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•  ๋•Œ JVM์ด ๋ฉ”๋ชจ๋ฆฌ์— ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ๋ณ€์ˆ˜๋ฅผ ๊ฐ’๊ณผ ์—ฐ๊ฒฐํ•˜๊ณ  ๊ฐ์ฒด๋ฅผ ๋ฉ”์„œ๋“œ์™€ ์—ฐ๊ฒฐํ•œ๋‹ค๊ณ  ๋Œ€๋‹ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ํด๋ž˜์Šค๊ฐ€ ์ƒ์†๋˜๊ฑฐ๋‚˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ์žฌ์ •์˜๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์žˆ๋Š” ๊ฒฝ์šฐ ์ด ํ˜•์‹์— ๋ฐ”์ธ๋”ฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ผ์น˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ๊ฒ€์ƒ‰์€ ํ•œ ๋‹จ๊ณ„ ๋†’์€ ํด๋ž˜์Šค(๋ถ€๋ชจ)๋กœ ์ด๋™ํ•˜๋Š” ์‹์œผ๋กœ ๋‹ค๋‹จ๊ณ„ ๊ณ„์ธต ๊ตฌ์กฐ์˜ ๋ฃจํŠธ๊นŒ์ง€ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. OOP์˜ ๋‹คํ˜•์„ฑ๊ณผ ์ฝ”๋“œ์—์„œ์˜ ๊ตฌํ˜„์— ๊ด€ํ•ด์„œ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ์ถ”์ƒ ์ •์˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ถ”์ƒํ™” ์›์น™(์ผ๋ฐ˜์ ์ธ ๋™์ž‘ ๋ฐ ์†์„ฑ์„ ์‹๋ณ„ํ•˜์—ฌ ์ถ”์ƒ ํด๋ž˜์Šค์— ๋„ฃ๊ฑฐ๋‚˜ ์ผ๋ฐ˜์ ์ธ ๋™์ž‘๋งŒ ์‹๋ณ„ํ•˜์—ฌ ์ธํ„ฐํŽ˜์ด์Šค์— ๋„ฃ๊ธฐ)์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ํด๋ž˜์Šค ์ƒ์†์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ์ฒด ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜๊ณ  ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Java์˜ ๋‹คํ˜•์„ฑ ๋ฐ ํ˜์‹ ๊ณผ ๊ด€๋ จํ•˜์—ฌ Java 8๋ถ€ํ„ฐ ์ถ”์ƒ ํด๋ž˜์Šค ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ ๋˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋™์ž‘๋งŒ ์‹๋ณ„ํ•˜๊ณ  ์ธํ„ฐํŽ˜์ด์Šค์— ๋„ฃ์Šต๋‹ˆ๋‹ค. ๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ํด๋ž˜์Šค ์ƒ์†์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ์ฒด ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜๊ณ  ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Java์˜ ๋‹คํ˜•์„ฑ ๋ฐ ํ˜์‹ ๊ณผ ๊ด€๋ จํ•˜์—ฌ Java 8๋ถ€ํ„ฐ ์ถ”์ƒ ํด๋ž˜์Šค ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ ๋˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋™์ž‘๋งŒ ์‹๋ณ„ํ•˜๊ณ  ์ธํ„ฐํŽ˜์ด์Šค์— ๋„ฃ์Šต๋‹ˆ๋‹ค. ๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ํด๋ž˜์Šค ์ƒ์†์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ์ฒด ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜๊ณ  ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Java์˜ ๋‹คํ˜•์„ฑ ๋ฐ ํ˜์‹ ๊ณผ ๊ด€๋ จํ•˜์—ฌ Java 8๋ถ€ํ„ฐ ์ถ”์ƒ ํด๋ž˜์Šค ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ์ถ”์ƒ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ๊ตฌํ˜„์„ ์ž‘์„ฑํ•˜๋Š” default ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

public interface CanSwim {
    default void swim() {
        System.out.println("I just swim");
    }
}
๋•Œ๋•Œ๋กœ ๋ฉด์ ‘๊ด€์€ ๋‹คํ˜•์„ฑ ์›์น™์ด ์œ„๋ฐ˜๋˜์ง€ ์•Š๋„๋ก ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•ด์•ผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์งˆ๋ฌธํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋‹ต์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฉ”์„œ๋“œ๋Š” static , private ๋˜๋Š” final ์ด ์•„๋‹ˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค . Private์€ ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ฏ€๋กœ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ •์ ์€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค์™€ ์—ฐ๊ฒฐํ•˜๋ฏ€๋กœ ์Šˆํผํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ•ญ์ƒ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  final์€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ณ  ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ˆจ๊ธธ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๋‹คํ˜•์„ฑ์€ ์šฐ๋ฆฌ์—๊ฒŒ ๋ฌด์—‡์„ ์ œ๊ณตํ•ฉ๋‹ˆ๊นŒ?

๋˜ํ•œ ๋‹คํ˜•์„ฑ์ด ์šฐ๋ฆฌ์—๊ฒŒ ์–ด๋–ค ์ด์ ์ด ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์งˆ๋ฌธ์„ ๋ฐ›์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ์„ธ๋ถ€ ์‚ฌํ•ญ์— ์–ฝ๋งค์ด์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํžˆ ๋Œ€๋‹ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  1. ํด๋ž˜์Šค ๊ตฌํ˜„์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ๋Š” ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.
  2. ํ™•์žฅ์„ฑ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜์—ฌ ๋ฏธ๋ž˜์— ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฐ˜์„ ํ›จ์”ฌ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด ์œ ํ˜•์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ ์œ ํ˜•์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ OOP ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
  3. ๊ณตํ†ต ์œ ํ˜• ๋˜๋Š” ๋™์ž‘์„ ๊ณต์œ ํ•˜๋Š” ๊ฐœ์ฒด๋ฅผ ํ•˜๋‚˜์˜ ์ปฌ๋ ‰์…˜ ๋˜๋Š” ๋ฐฐ์—ด๋กœ ๊ฒฐํ•ฉํ•˜๊ณ  ๊ท ์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์˜ˆ์ œ์—์„œ์™€ ๊ฐ™์ด ๋ชจ๋“  ์‚ฌ๋žŒ์ด ๊ฐ•์ œ๋กœ dance() ๋˜๋Š” swim() ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค :)
  4. ์ƒˆ๋กœ์šด ์œ ํ˜• ์ƒ์„ฑ์˜ ์œ ์—ฐ์„ฑ: ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„์„ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ผ๋ถ€ ์ด๋ณ„์˜ ๋ง

๋‹คํ˜•์„ฑ์€ ๋งค์šฐ ์ค‘์š”ํ•˜๊ณ  ๊ด‘๋ฒ”์œ„ํ•œ ์ฃผ์ œ์ž…๋‹ˆ๋‹ค. Java์˜ OOP์— ๋Œ€ํ•œ ์ด ๊ธฐ์‚ฌ์˜ ๊ฑฐ์˜ ์ ˆ๋ฐ˜์˜ ์ฃผ์ œ์ด๋ฉฐ ์–ธ์–ด ๊ธฐ๋ฐ˜์˜ ์ƒ๋‹น ๋ถ€๋ถ„์„ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ธํ„ฐ๋ทฐ์—์„œ ์ด ์›์น™์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ํ”ผํ•  ์ˆ˜ ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ฅด๊ฑฐ๋‚˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜๋ฉด ์•„๋งˆ ๋ฉด์ ‘์ด ๋๋‚  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ฒŒ์œผ๋ฅด์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ์ธํ„ฐ๋ทฐ ์ „์— ์ง€์‹์„ ํ‰๊ฐ€ํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ƒˆ๋กœ ๊ณ ์น˜์‹ญ์‹œ์˜ค.