๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Language/Javascript

[Javascript]์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ super์™€ override ์™„๋ฒฝ ์ดํ•ดํ•˜๊ธฐ

by YJ Dev 2024. 6. 28.
728x90
๋ฐ˜์‘ํ˜•
SMALL

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP)์„ ์ง€์›ํ•˜๋ฉฐ, ์ด๋Š” ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํ•ต์‹ฌ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ƒ์†(inheritance)์ด๋ฉฐ, ์ด๋•Œ super์™€ override๊ฐ€ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” super์™€ override์˜ ๊ฐœ๋…๊ณผ ์‚ฌ์šฉ๋ฒ•์„ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ ๋‹จ๊ณ„ ๋” ๊นŠ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ super override


์ƒ์†์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”๐Ÿ˜

""

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ]javascript ์ƒ์† ์™„๋ฒฝ ๊ฐ€์ด๋“œ: ๊ธฐ๋ณธ ๊ฐœ๋…๋ถ€ํ„ฐ ์‹ค์ „ ์˜ˆ์ œ๊นŒ์ง€

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP)์˜ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ์ƒ์† ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ์œ ์—ฐ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š”

creativevista.tistory.com


01. super ํ‚ค์›Œ๋“œ์˜ ๊ฐœ๋…๊ณผ ํ™œ์šฉ๐Ÿ’ก

super์˜ ์ •์˜

super๋Š” ES6(ECMAScript 2015)์—์„œ ๋„์ž…๋œ ํ‚ค์›Œ๋“œ๋กœ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํด๋ž˜์Šค ์ƒ์†์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ๋•Œ super๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


super๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์ƒ์†์„ ํ†ตํ•ด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด super๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ ์ค‘๋ณต์„ ์ค„์ด๊ณ , ํด๋ž˜์Šค ๊ฐ„์˜ ๋ช…ํ™•ํ•œ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


super์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• ์˜ˆ์ œ ์ฝ”๋“œ

๋‹ค์Œ์€ super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” Parent ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ๋ฅผ Child ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

class Parent {
    constructor(name) {
        this.name = name;
    }

    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
}

class Child extends Parent {
    constructor(name, age) {
        super(name); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        this.age = age;
    }

    greet() {
        super.greet(); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log(`I am ${this.age} years old`);
    }
}

const child = new Child('John', 25);
child.greet();
// ์ถœ๋ ฅ:
// Hello, my name is John
// I am 25 years old

์ƒ์† ๊ด€๊ณ„์—์„œ์˜ super ํ™œ์šฉ

์œ„ ์˜ˆ์ œ์—์„œ super(name)์€ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ name ์†์„ฑ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ super.greet()์€ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ super๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


super๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜์‚ฌํ•ญ

  • super๋Š” ์ž์‹ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ๋‚ด์—์„œ ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ๋ฐ˜๋“œ์‹œ this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • super๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ super๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฌธ๋ฒ• ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

02. override(์˜ค๋ฒ„๋ผ์ด๋“œ)์˜ ๊ฐœ๋…๊ณผ ํ™œ์šฉ๐Ÿ”„

์˜ค๋ฒ„๋ผ์ด๋“œ์˜ ์ •์˜

์˜ค๋ฒ„๋ผ์ด๋“œ๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ์˜ ํ•„์š”์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋“œ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋‹คํ˜•์„ฑ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ์˜ ํ•„์š”์„ฑ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํด๋ž˜์Šค ์ƒ์†์—์„œ ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠน์ • ๋ฉ”์„œ๋“œ์˜ ๋™์ž‘์„ ์ž์‹ ํด๋ž˜์Šค์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ๊ฒฝ์šฐ, ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋™์ž‘์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ค๋ฒ„๋ผ์ด๋“œ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• ์˜ˆ์ œ ์ฝ”๋“œ

๋‹ค์Œ์€ ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

class Parent {
    greet() {
        console.log('Hello from Parent');
    }
}

class Child extends Parent {
    greet() {
        console.log('Hello from Child');
    }
}

const child = new Child();
child.greet(); // ์ถœ๋ ฅ: Hello from Child

์œ„ ์˜ˆ์ œ์—์„œ Child ํด๋ž˜์Šค๋Š” Parent ํด๋ž˜์Šค์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ์ž์‹ ๋งŒ์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ Child ๊ฐ์ฒด์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹Œ ์ž์‹ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.


override ์ฃผ์˜์‚ฌํ•ญ ๋ฐ ํŒ

  • ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•  ๋•Œ๋Š” ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜(๋ฉ”์„œ๋“œ ์ด๋ฆ„๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ)๊ฐ€ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ค๋ฒ„๋ผ์ด๋“œ๋œ ๋ฉ”์„œ๋“œ์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ค๋ฒ„๋ผ์ด๋“œ๋Š” ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ณ , ํด๋ž˜์Šค ๊ฐ„์˜ ๋ช…ํ™•ํ•œ ์—ญํ•  ๋ถ„๋‹ด์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

03. super์™€ override๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ๐ŸŽฏ

super์™€ override๋ฅผ ์กฐํ•ฉํ•œ ์‹ค์ „ ์˜ˆ์ œ

super์™€ override๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ์žฌ์ •์˜ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๋ฉด์„œ, ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ super๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœํ•˜์—ฌ ๊ธฐ์กด ๊ธฐ๋Šฅ์„ ์œ ์ง€ํ•˜๊ณ  ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์ œ๋Š” super์™€ override๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

class Parent {
    greet() {
        console.log('Hello from Parent');
    }
}

class Child extends Parent {
    greet() {
        super.greet(); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log('Hello from Child');
    }
}

const child = new Child();
child.greet();
// ์ถœ๋ ฅ:
// Hello from Parent
// Hello from Child

์ด ์˜ˆ์ œ์—์„œ Child ํด๋ž˜์Šค๋Š” Parent ํด๋ž˜์Šค์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๊ณ , super.greet()์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ›„, ์ถ”๊ฐ€๋กœ "Hello from Child"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.


์ƒ์† ๊ด€๊ณ„์—์„œ์˜ super ํ˜ธ์ถœ๊ณผ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ

์ƒ์† ๊ด€๊ณ„์—์„œ super์™€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜๋ฉด ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ๋” ๋ณต์žกํ•œ ์ƒ์† ๊ตฌ์กฐ์—์„œ super์™€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

class Animal {
    makeSound() {
        console.log('Some generic animal sound');
    }
}

class Dog extends Animal {
    makeSound() {
        super.makeSound(); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log('Bark');
    }
}

class Labrador extends Dog {
    makeSound() {
        super.makeSound(); // ๋ถ€๋ชจ ํด๋ž˜์Šค(Dog)์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log('Woof');
    }
}

const myLabrador = new Labrador();
myLabrador.makeSound();
// ์ถœ๋ ฅ:
// Some generic animal sound
// Bark
// Woof

์œ„ ์˜ˆ์ œ์—์„œ Labrador ํด๋ž˜์Šค๋Š” Dog ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ , Dog ํด๋ž˜์Šค๋Š” Animal ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. Labrador ํด๋ž˜์Šค์—์„œ makeSound ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๊ณ  super.makeSound()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Dog ํด๋ž˜์Šค์˜ makeSound ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ›„, ์ถ”๊ฐ€๋กœ "Woof"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. Dog ํด๋ž˜์Šค์˜ makeSound ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์‹œ Animal ํด๋ž˜์Šค์˜ makeSound ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ "Some generic animal sound"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.


์ฝ”๋“œ๋กœ ์ดํ•ดํ•˜๋Š” ๋ณต์žกํ•œ ์ƒ์† ๊ตฌ์กฐ

๋ณต์žกํ•œ ์ƒ์† ๊ตฌ์กฐ์—์„œ๋„ super์™€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ž˜์Šค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋ช…ํ™•ํžˆ ํ•˜๊ณ  ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์˜ ์ƒ์† ๊ตฌ์กฐ์—์„œ super์™€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

class Vehicle {
    start() {
        console.log('Starting the vehicle');
    }
}

class Car extends Vehicle {
    start() {
        super.start(); // ๋ถ€๋ชจ ํด๋ž˜์Šค(Vehicle)์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log('Starting the car');
    }
}

class ElectricCar extends Car {
    start() {
        super.start(); // ๋ถ€๋ชจ ํด๋ž˜์Šค(Car)์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log('Starting the electric car');
    }
}

const myElectricCar = new ElectricCar();
myElectricCar.start();
// ์ถœ๋ ฅ:
// Starting the vehicle
// Starting the car
// Starting the electric car

์œ„ ์˜ˆ์ œ์—์„œ ElectricCar ํด๋ž˜์Šค๋Š” Car ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ , Car ํด๋ž˜์Šค๋Š” Vehicle ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ElectricCar ํด๋ž˜์Šค์˜ start ๋ฉ”์„œ๋“œ๋Š” super.start()๋ฅผ ํ†ตํ•ด Car ํด๋ž˜์Šค์˜ start ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , Car ํด๋ž˜์Šค์˜ start ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์‹œ Vehicle ํด๋ž˜์Šค์˜ start ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ์จ ๊ฐ ํด๋ž˜์Šค์˜ start ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.


04. ๊ฒฐ๋ก  ๋ฐ ์š”์•ฝ๐Ÿ“š

super์™€ override์˜ ํ•ต์‹ฌ ์š”์•ฝ

super์™€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ค‘์š”ํ•œ ๊ฐœ๋…์œผ๋กœ, ์ƒ์† ๊ด€๊ณ„์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์™€ ์ž์‹ ํด๋ž˜์Šค ๊ฐ„์˜ ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

super

  • ์ •์˜: ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค์›Œ๋“œ.
  • ์‚ฌ์šฉ ์ด์œ : ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • ์˜ˆ์ œ
class Parent {
    constructor(name) {
        this.name = name;
    }
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    }
}

class Child extends Parent {
    constructor(name, age) {
        super(name); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
        this.age = age;
    }

    greet() {
        super.greet(); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log(`I am ${this.age} years old`);
    }
}

const child = new Child('John', 25);
child.greet();
// ์ถœ๋ ฅ:
// Hello, my name is John
// I am 25 years old

override (์˜ค๋ฒ„๋ผ์ด๋“œ)

  • ์ •์˜: ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒƒ.
  • ์‚ฌ์šฉ ์ด์œ : ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ์˜ ์š”๊ตฌ์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • ์˜ˆ์ œ
class Parent {
    greet() {
        console.log('Hello from Parent');
    }
}

class Child extends Parent {
    greet() {
        console.log('Hello from Child');
    }
}

const child = new Child();
child.greet(); // ์ถœ๋ ฅ: Hello from Child

super์™€ override๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

  • ์ •์˜: ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๋ฉด์„œ super๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ.
  • ์‚ฌ์šฉ ์ด์œ : ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€์ ์ธ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • ์˜ˆ์ œ
class Parent {
    greet() {
        console.log('Hello from Parent');
    }
}

class Child extends Parent {
    greet() {
        super.greet(); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        console.log('Hello from Child');
    }
}

const child = new Child();
child.greet();
// ์ถœ๋ ฅ:
// Hello from Parent
// Hello from Child

๊ฒฐ๋ก 

super์™€ ์˜ค๋ฒ„๋ผ์ด๋“œ๋Š” ์ƒ์†์„ ํ†ตํ•ด ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉํ•˜๊ณ , ์ž์‹ ํด๋ž˜์Šค์˜ ์š”๊ตฌ์— ๋งž๊ฒŒ ๊ธฐ๋Šฅ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ณ , ๋ช…ํ™•ํ•œ ํด๋ž˜์Šค ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
LIST