๐™น๐šŠ๐šŸ๐šŠ

[Java] ์˜ค๋ฒ„๋ผ์ด๋”ฉ(override)์ด๋ž€? ์˜ค๋ฒ„๋ผ์ด๋”ฉ vs ์˜ค๋ฒ„๋กœ๋”ฉ / ์˜ค๋ฒ„๋ผ์ด๋”ฉ์˜ ์กฐ๊ฑด

ํ•ด๋ฒ„๋‹ˆ 2024. 5. 3. 11:10
๋ฐ˜์‘ํ˜•

์˜ค๋ฒ„๋ผ์ด๋”ฉ

 

 

์˜ค๋ฒ„๋ผ์ด๋”ฉ์˜ ์‚ฌ์ „์  ์˜๋ฏธ๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? 

override : ~์œ„์— ๋ฎ์–ด์“ฐ๋‹ค(overwrite)
์กฐ์ƒํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ๋ฉ”์„œ๋“œ์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ


์ƒ์†๋ฐ›์€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์ž์† ํด๋ž˜์Šค ์ž์‹ ์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
๊ทธ๋Ÿด๋•Œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด ์“ฐ์ธ๋‹ค. 

 

 

 

2์ฐจ์› x,y ์ขŒํ‘œ์— ๋Œ€ํ•œ ํด๋ž˜์Šค Point์™€ 3์ฐจ์› x, y, z์ขŒํ‘œ์— ๋Œ€ํ•œ ํด๋ž˜์Šค Point3D์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

class Point {
    int x;
    int y;

    String getLocation() {
        return "x :" + x + ", y :"+ y;
    }
}

class Point3D extends Point { // Point ์ƒ์† ๋ฐ›์Œ
    int z;

    String getLocation() { // ์˜ค๋ฒ„๋ผ์ด๋”ฉ
        return "x :" + x + ", y :"+ y + ", z :"+z;
    }
}

ํด๋ž˜์Šค Point๋Š” x, y ์ขŒํ‘œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ 3D Point๋Š” x, y, z ์„ธ ๊ฐœ์˜ ์ ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค Point์˜ getLocation()๋Š” x, y๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Point3D์— ๋งž๊ฒŒ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ํ•ด์คฌ๋‹ค.





๊ทธ๋Ÿผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์˜ ์กฐ๊ฑด์€ ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? 

์˜ค๋ฒ„๋ผ์ด๋”ฉ์˜ ์กฐ๊ฑด

๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ์กฐ๊ฑด

์ž์† ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ์กฐ์ƒ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์™€
- ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
- ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
- ๋ฐ˜ํ™˜ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.



์ด๋ฆ„, ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฐ˜ํ™”ํƒ€์ž… ์„ธ๊ฐ€์ง€๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. 

์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋„ 

์ด๋ฆ„ : getLocation

๋งค๊ฐœ๋ณ€์ˆ˜ : ์—†์Œ

๋ฐ˜ํ™˜ํƒ€์ž… : String

์œผ๋กœ ๋™์ผํ–ˆ๋‹ค.

 

 

 

 

์˜ค๋ฒ„๋ผ์ด๋”ฉ ์ œ์•ฝ

์กฐ์ƒ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์† ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ
1. ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ ์กฐ์ƒ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ณด๋‹ค ์ข์€ ๋ฒ”์œ„๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
2. ์˜ˆ์™ธ๋Š” ์กฐ์ƒ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ณด๋‹ค ๋งŽ์ด ์–ธ๊ธ‰ํ•  ์ˆ˜ ์—†๋‹ค.
3. ์ธ์Šคํ„ด์Šค๋ฉ”์„œ๋“œ๋ฅผ static ๋ฉ”์„œ๋“œ๋กœ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

 

3๊ฐ€์ง€ ์ œ์•ฝ์ด ์žˆ๋‹ค.

๊ฐ๊ฐ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž.

 

 

 

 

1๏ธโƒฃ ์ ‘๊ทผ ์ œ์–ด์ž๋Š” ์กฐ์ƒ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ณด๋‹ค ์ข์€ ๋ฒ”์œ„๋กœ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†๋‹ค.

class Parent {
    public void method1() {
        System.out.println("Parent์˜ method1");
    }

    protected void method2() {
        System.out.println("Parent์˜ method2");
    }
}

class Child extends Parent {
    // public -> protected๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
    // ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ
    @Override
    protected void method1() {
        System.out.println("Child์˜ method1");
    }

    // ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ ๋„“์€ ๋ฒ”์œ„๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋œ๋‹ค.
    // protected -> public์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
    @Override
    public void method2() {
        System.out.println("Child์˜ method2");
    }
}

 

์ ‘๊ทผ ์ œ์–ด์ž๋Š” ๋„“์€ ๋ฒ”์œ„๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€๋งŒ, ์ข์€ ๋ฒ”์œ„๋กœ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. 
public > protected > (default)> private
public์ด ๊ฐ€์žฅ ๋„“์€ ๋ฒ”์œ„์ด๊ณ , private์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ์ข์€ ๋ฒ”์œ„์ด๋‹ค. 

 

 

 

 

 

2๏ธโƒฃ ์กฐ์ƒ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ณด๋‹ค ๋งŽ์€ ์ˆ˜์˜ ์˜ˆ์™ธ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.

class Parent {
    public void doSomething() throws IOException {
        // ...
    }
}

class Child extends Parent {
    // ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ!
    // ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋” ๋งŽ์€ ์˜ˆ์™ธ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.
    public void doSomething() throws IOException, SQLException {
        // ...
    }
}

์กฐ์ƒ ํด๋ž˜์Šค์—์„  IOException๋งŒ ์˜ˆ์™ธ๋ฅผ ์„ ์–ธํ–ˆ๊ณ , ์ž์‹ ํด๋ž˜์Šค์—์„  IOException, SQLException์„ ์„ ์–ธํ–ˆ๋‹ค.
์กฐ์ƒ๊ณผ ๋™์ผํ•œ ์˜ˆ์™ธ๋งŒ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

 

 

 

โ“ ๊ทผ๋ฐ SQLException๋„ ์“ฐ๊ณ  ์‹ถ์€๋ฐ ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด โ“

class Child extends Parent {
    public void doSomething() throws IOException {
        try {
            // ...
        } catch (SQLException e) {
            // SQLException ์ฒ˜๋ฆฌ
        }
    }
}

๊ทธ๋Ÿด ๋•Œ๋Š” try-catch๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

 

 

 

3๏ธโƒฃ ์ธ์Šคํ„ด์Šค๋ฉ”์„œ๋“œ๋ฅผ static ๋ฉ”์„œ๋“œ๋กœ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

 

์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ → static ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ

class Parent {
    // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
    public void instanceMethod() {
        System.out.println("๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ");
    }
}

class Child extends Parent {
    // static ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ์‹œ๋„
    // ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ!
    public static void instanceMethod() {
        System.out.println("์ž์‹ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ");
    }
}

instance ๋ฉ”์„œ๋“œ๋ฅผ static ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

 

 

 

static ๋ฉ”์„œ๋“œ→ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ

class Parent {
    // static ๋ฉ”์„œ๋“œ
    public static void staticMethod() {
        System.out.println("๋ถ€๋ชจ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ");
    }
}

class Child extends Parent {
    // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ์‹œ๋„
    // ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ!
    public void staticMethod() {
        System.out.println("์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ");
    }
}

static ๋ฉ”์„œ๋“œ๋ฅผ instance ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. 

 

 

 

 

import java.io.*;

class Parent {
    public static void staticMethod() {
        System.out.println("๋ถ€๋ชจ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ");
    }

    public void instanceMethod() {
        System.out.println("๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ");
    }
}

class Child extends Parent {
    // ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด static ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ ๊ฒƒ์ด๋‹ค.
    public static void staticMethod() {
        System.out.println("์ž์‹ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ");
    }

    // ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด๋‹ค.
    @Override
    public void instanceMethod() {
        System.out.println("์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ");
    }
}

public class Main {
    public static void main(String[] args) throws IOException {
        Parent.staticMethod(); // "๋ถ€๋ชจ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ"
        Child.staticMethod();  // "์ž์‹ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ"

        Parent parent = new Parent();
        parent.instanceMethod(); // "๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ"

        Child child = new Child();
        child.instanceMethod(); // "์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ"
    }
}

/* ์ถœ๋ ฅ 
๋ถ€๋ชจ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ
์ž์‹ ํด๋ž˜์Šค์˜ static ๋ฉ”์„œ๋“œ
๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
*/

ํ˜ธ์ถœํ• ๋•Œ์—”

static์„ ์“ด๋‹ค๋ฉด ํด๋ž˜์Šค์ด๋ฆ„.๋ฉ”์„œ๋“œ์ด๋ฆ„() ์ด๋ผ๊ณ  ์จ์•ผ ํ•œ๋‹ค.

์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ํ•œ๋‹ค๋ฉด ์ฐธ์กฐ๋ณ€์ˆ˜.๋ฉ”์„œ๋“œ์ด๋ฆ„() ์ด๋ผ๊ณ  ์จ์•ผ ํ•œ๋‹ค.




 

static

  • static์€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šคํ™” ์—†์ด ์–ด๋””์„œ๋“  ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

→ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค์™€๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

  • static์€ ํด๋ž˜์Šค ๋ ˆ๋ฒจ์—์„œ ๋™์ž‘ํ•œ๋‹ค.

 

์ธ์Šคํ„ด์Šค

  • ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด ๋ ˆ๋ฒจ์—์„œ ๋™์ž‘

์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ํ•  ๋•Œ์—” static์„ ์จ์„  ์•ˆ ๋œ๋‹ค.






 

 

์˜ค๋ฒ„๋กœ๋”ฉ vs ์˜ค๋ฒ„๋ผ์ด๋”ฉ

์˜ค๋ฒ„๋กœ๋”ฉ(overloading) ๊ธฐ์กด์— ์—†๋Š” ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ(new)
์˜ค๋ฒ„๋ผ์ด๋”ฉ(overriding) ์ƒ์†๋ฐ›์€ ๋ฉ”์„œ๋“œ์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ(change, modify)

 

class Parent {
    void parentMethod() {}
}

class Child extends Parent {
    void parentMethod() {} // ์˜ค๋ฒ„๋ผ์ด๋”ฉ
    void parentMethod(int i) {} // ์˜ค๋ฒ„๋กœ๋”ฉ

    void childMethod() {}
    void childMethod(int i) {} // ์˜ค๋ฒ„๋กœ๋”ฉ
}

 

 




Animal ์˜ˆ์ œ ์ฝ”๋“œ

class Animal {
    // ์˜ค๋ฒ„๋กœ๋”ฉ: makeSound ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒ„์ „์œผ๋กœ ์ •์˜
    public void makeSound() {
        System.out.println("๋™๋ฌผ์ด ์†Œ๋ฆฌ๋ฅผ ๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.");
    }
    
    // ์˜ค๋ฒ„๋กœ๋”ฉ: makeSound ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒ„์ „์œผ๋กœ ์ •์˜
    public void makeSound(String sound) {
        System.out.println("๋™๋ฌผ์ด " + sound + " ์†Œ๋ฆฌ๋ฅผ ๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.");
    }
}

class Dog extends Animal {
    // ์˜ค๋ฒ„๋ผ์ด๋”ฉ: ์ƒ์œ„ ํด๋ž˜์Šค์˜ makeSound ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜
    @Override
    public void makeSound() {
        System.out.println("๋ฉ๋ฉ!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.makeSound(); // "๋™๋ฌผ์ด ์†Œ๋ฆฌ๋ฅผ ๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค."
        animal.makeSound("๋ผ๋ฃฉ"); // "๋™๋ฌผ์ด ๋ผ๋ฃฉ ์†Œ๋ฆฌ๋ฅผ ๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค."
        
        Dog dog = new Dog();
        dog.makeSound();    // "๋ฉ๋ฉ!"
        // dog.makeSound("๋ผ๋ฃฉ"); // ์ปดํŒŒ์ผ ์—๋Ÿฌ: Dog ํด๋ž˜์Šค์—๋Š” makeSound(String sound) ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์Œ
    }
}

 

 

 

 

super

์ž์† ํด๋ž˜์Šค์—์„œ ์กฐ์ƒ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„๋ฅผ ์ฐธ์กฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๋‹ค.

this๋ฅผ ์‚ฌ์šฉํ–ˆ๋“ฏ์ด ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„์™€ ์ž์‹ ์˜ ๋ฉค๋ฒ„์™€ ์ด๋ฆ„์ด ๊ฐ™์„ ๋•Œ๋Š” super๋ฅผ ๋ถ™์—ฌ์„œ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

class Parent {
    int x=10;
}

class Child extends Parent {
    void method() {
        System.out.println("x=" + x);
        System.out.println("this.x=" + this.x);
        System.out.println("super.x=" + super.x); 
    }
}

class SuperTest {
    public static void main(String args[]) {
        Child c = new Child();
        c.method();
    }
}

/* 
x=10
this.x=10
super.x=10
*/

 

 

 

class Parent {
    int x=10;
}

class Child extends Parent {
    int x=20;

    void method() {
        System.out.println("x=" + x);
        System.out.println("this.x=" + this.x);
        System.out.println("super.x=" + super.x); 
    }
}

class SuperTest {
    public static void main(String args[]) {
        Child c = new Child();
        c.method();
    }
}

/* 
x=20
this.x=20
super.x=10
*/



๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๊ฐ€ Parent์—๋„ ์žˆ๊ณ , Child์—๋„ ์žˆ์„ ๋•Œ๋Š” super.x์™€ this.x๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.

super.x → ์กฐ์ƒ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„๋ณ€์ˆ˜ x

this.x → ์ž์† ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ๋ฉค๋ฒ„๋ณ€์ˆ˜ x






 

 

super() - ์กฐ์ƒ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž

this()์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ super() ์—ญ์‹œ ์ƒ์„ฑ์ž์ด๋‹ค.

this()→ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

super() → ์กฐ์ƒ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ



 

Object ํด๋ž˜์Šค๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ์ฒซ ์ค„์— ์ƒ์„ฑ์ž,this() ๋˜๋Š” super(),๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์ ์œผ๋กœ super();๋ฅผ ์ƒ์„ฑ์ž์˜ ์ฒซ ์ค„์— ์‚ฝ์ž…ํ•œ๋‹ค.

 

 

class Parent {
    public Parent() {
        System.out.println("๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž");
    }
}

class Child extends Parent {
    public Child() {
        // ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ super();๋ฅผ ์ƒ์„ฑ์ž์˜ ์ฒซ ์ค„์— ์‚ฝ์ž…
        // ๋”ฐ๋ผ์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋จ
        System.out.println("์ž์‹ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}

Child ํด๋ž˜์Šค์— ์ƒ์„ฑ์ž ์ •์˜๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด super();์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค. 

์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ๋งŒํผ ์ƒ์„ฑ์ž๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜๋‹ค

 



ํด๋ž˜์Šค์™€ ์ƒ์„ฑ์ž์˜ ์—ญํ•  ๋ฐ ๊ด€๊ณ„ 
1๏ธโƒฃ ํด๋ž˜์Šค - ์–ด๋–ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ธ๊ฐ€?
2๏ธโƒฃ ์ƒ์„ฑ์ž - ์„ ํƒํ•œ ํด๋ž˜์Šค์˜ ์–ด๋–ค ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ธ๊ฐ€?




 

// Car ํด๋ž˜์Šค ์ •์˜
public class Car {
    private String model;
    private String color;
    private int speed;

    // ์ƒ์„ฑ์ž - ๋ชจ๋ธ๊ณผ ์ƒ‰์ƒ์„ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž
    public Car(String model, String color) {
        this.model = model;
        this.color = color;
        this.speed = 0; // ์ดˆ๊ธฐ ์†๋„๋Š” 0์œผ๋กœ ์„ค์ •
    }

    // ์ƒ์„ฑ์ž - ๋ชจ๋ธ, ์ƒ‰์ƒ, ์ดˆ๊ธฐ ์†๋„๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž
    public Car(String model, String color, int speed) {
        this.model = model;
        this.color = color;
        this.speed = speed;
    }

    // Getter์™€ Setter ๋ฉ”์„œ๋“œ ์ƒ๋žต
}

// ๋ฉ”์ธ ํด๋ž˜์Šค
public class Main {
    public static void main(String[] args) {
        // Car ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ์„ ํƒํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
        Car car1 = new Car("Toyota", "Red");
        Car car2 = new Car("Tesla", "Black", 100);

        // ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค์˜ ์ •๋ณด ์ถœ๋ ฅ
        System.out.println("Car 1: Model - " + car1.getModel() + ", Color - " + car1.getColor() + ", Speed - " + car1.getSpeed());
        System.out.println("Car 2: Model - " + car2.getModel() + ", Color - " + car2.getColor() + ", Speed - " + car2.getSpeed());
    }
}

์—ฌ๊ธฐ์„œ ์–ด๋–ค ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ• ์ง€ ์ž˜ ์„ ํƒ์„ ํ•ด์•ผ ํ•œ๋‹ค.

speed๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ๋‘๋ฒˆ์งธ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.





class Point {
    int x = 10;
    int y = 20;

    Point(int x, int y){
        this.x = x; // ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์œ„์— super() ์ž๋™ ์ƒ์„ฑ -> Object()๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
        this.y = y; 
    }
}

class Point3D extend Point {
    int z = 30;

    Point3D() {
        this(100, 200, 300);
    }

    Point3D(int x, int y, int z) {
        super(x,y);
        this.z = z;
    }
}

class PointTest2 {
    public static void main(String args[]) {
        Point3D p3 = new Point3D();
        System.out.println("p.3x=" + p3.x);
        System.out.println("p.3y=" + p3.y);
        System.out.println("p.3z=" + p3.z);
    }
}

/* 
    p3.x=100
    p3.y=200
    p3.z=300
*/






๋ฐ˜์‘ํ˜•