๐Ÿ Python vs โ˜• Java: ์ถ”์ƒ ํด๋ž˜์Šค์™€ ๋‹คํ˜•์„ฑ ๋น„๊ต

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ ๊ฐœ๋…์ธ ์ƒ์†๊ณผ ๋‹คํ˜•์„ฑ, ๊ทธ๋ฆฌ๊ณ  ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ž์ฃผ ์ ‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Python๊ณผ Java์—์„œ ์ด ๊ฐœ๋…๋“ค์ด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜๋Š”์ง€, ๋‘ ์–ธ์–ด์˜ ์ฐจ์ด๋ฅผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋กœ ๋น„๊ตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๐Ÿ”ธ ์˜ˆ์‹œ ์‹œ๋‚˜๋ฆฌ์˜ค: ๋™๋ฌผ์ด ๋ง์„ ํ•œ๋‹ค?

Python ์ฝ”๋“œ

class Animal:
def __init__(self, name, age):
self.name = name
self.age = age

def speak(self):
raise NotImplementedError("Subclasses must implement this method")

class Dog(Animal):
def __init__(self, name, age, breed):
super().__init__(name, age)
self.breed = breed

def speak(self):
print("Woof!")

class Cat(Animal):
def __init__(self, name, age, color):
super().__init__(name, age)
self.color = color

def speak(self):
print("Meow!")

animals = [
Animal("Generic Animal", 5),
Dog("Buddy", 3, "Golden Retriever"),
Cat("Whiskers", 2, "Gray"),
]

for animal in animals:
try:
animal.speak()
except NotImplementedError:
print(f"{animal.name} cannot speak (abstract method)")

Java ์ฝ”๋“œ

abstract class Animal {
protected String name;
protected int age;

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

public abstract void speak();
}

class Dog extends Animal {
private String breed;

public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}

@Override
public void speak() {
System.out.println("Woof!");
}
}

class Cat extends Animal {
private String color;

public Cat(String name, int age, String color) {
super(name, age);
this.color = color;
}

@Override
public void speak() {
System.out.println("Meow!");
}
}

public class Main {
public static void main(String[] args) {
Animal[] animals = {
new Dog("Buddy", 3, "Golden Retriever"),
new Cat("Whiskers", 2, "Gray")
};

for (Animal animal : animals) {
animal.speak(); // ๋‹คํ˜•์„ฑ
}
}
}

๐Ÿ” ํ•ต์‹ฌ ๋น„๊ต ์š”์•ฝ

๊ฐœ๋…PythonJava
์ถ”์ƒ ํด๋ž˜์Šคraise NotImplementedError()abstract class, abstract method
์ƒ์„ฑ์ž__init__ ๋ฉ”์„œ๋“œํด๋ž˜์Šค๋ช…๊ณผ ๋™์ผํ•œ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ
๋‹คํ˜•์„ฑ ๊ตฌํ˜„๋ถ€๋ชจ ํด๋ž˜์Šค ๋ฆฌ์ŠคํŠธ์— ์ž์‹ ๊ฐ์ฒด ์ €์žฅ๋ถ€๋ชจ ํด๋ž˜์Šค ๋ฐฐ์—ด ๋˜๋Š” ๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ
์˜ˆ์™ธ ์ฒ˜๋ฆฌtry + except NotImplementedErrortry + catch (UnsupportedOperationException ๋“ฑ)

โœ… ์ •๋ฆฌ

  • Python์€ NotImplementedError๋ฅผ ํ†ตํ•ด ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ•์ œํ•˜๋ฉฐ, ์œ ์—ฐํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง
  • Java๋Š” ๋ช…ํ™•ํ•œ abstract ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ปดํŒŒ์ผ ํƒ€์ž„์— ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•จ
  • ๋‘ ์–ธ์–ด ๋ชจ๋‘ **๋‹คํ˜•์„ฑ(polymorphism)**์„ ํ†ตํ•ด ์ž์‹ ํด๋ž˜์Šค์˜ ํ–‰๋™์„ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

๐Ÿ”ง ์‹ค์ „ ํŒ:

  • Python์—์„œ๋Š” ABC(Abstract Base Class) ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•˜๋ฉด Java์ฒ˜๋Ÿผ ๋” ์—„๊ฒฉํ•œ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์š”!
  • Java์—์„œ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค ๋Œ€์‹  interface๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋” ์œ ์—ฐํ•œ ๊ตฌ์กฐ ์„ค๊ณ„๋„ ๊ฐ€๋Šฅํ•˜์ฃ .

์ฝ”๋ฉ˜ํŠธ

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค