상속은 한 클래스가 다른 클래스의 속성과 메서드를 취하는 프로세스이다. 새롭게 생성된 클래스를 자식 클래스라고 하고 다른 하나를 부모 클래스라고 한다.

아래의 코드를 보면 직원이라는 클래스에 소프트웨어 엔지니어와 디자이너가 있다. 이때 직원의 클래스를 부모 클래스라고하고 소프트웨어 엔지니어와 디자이너 클래스를 자식 클래스라고한다. 상속은 자식 클래스 이름 옆에 괄호()를 이용하여 구현한다.

class Employee:
    pass

class SoftwareEngineer(Employee):
    pass

class Designer(Employee):
    pass

아래의 코드를 보면 자식클래스에서 부모클래스에 접근을 하여 잘 작동하는 것을 알 수 있고, 부모클래스를 받아 사용한다는 의미로 상속이라고 한다.

class Employee:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def work(self):
        print(f"{self.name} is working")

class SoftwareEngineer(Employee):
    pass

class Designer(Employee):
    pass

se1 = SoftwareEngineer("Max" ,25)
se1.work()

d = Designer("Philipp", 27)
d.work()

확장과 재정의

먼저 확장을 알아보면 부모에서 사용했던 코드에 추가적으로 더하는 것을 의미한다. 아래의 코드도 부모 클래스에서 3가지를 초기화 하지만 자식클래스에서 level을 추가적으로 초기화하고 있다. 또한 상위 클래스에 접근을 한다는 의미인 super()을 사용해야 한다.

class Employee:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary
    
    def work(self):
        print(f"{self.name} is working")

class SoftwareEngineer(Employee):
    def __init__(self, name, age, salary, level):
        super().__init__(name, age, salary)
        self.level = level

class Designer(Employee):
    pass

se1 = SoftwareEngineer("Max" ,25, 6000 , "Junior")
se1.work()

d = Designer("Philipp", 27, 7000)
d.work()

재정의는 같은 함수의 이름이 부모와 자식에 둘 다 있을 때 가까운 함수를 불러온다. 아래의 코드는 부모함수의 work가 아닌 자식 함수에서 work를 불러오는 것을 알 수 있다.

class Employee:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary
    
    def work(self):
        print(f"{self.name} is working")

class SoftwareEngineer(Employee):
    def __init__(self, name, age, salary, level):
        super().__init__(name, age, salary)
        self.level = level
    
    def work(self):
        print(f"{self.name} is coding")

class Designer(Employee):
    def work(self):
        print(f"{self.name} is drawing")
    

se1 = SoftwareEngineer("Max" ,25, 6000 , "Junior")
se1.work()

d = Designer("Philipp", 27, 7000)
d.work()

다형성

다형성은 같은 모양의 코드가 다른 동작을 하는 것이다. for문을 보면 work라는 같은 코드를 실행하지만 재정의에 의해서 다른 코드가 실행되는 것을 알 수 있다.

class Employee:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary
    
    def work(self):
        print(f"{self.name} is working")

class SoftwareEngineer(Employee):
    def __init__(self, name, age, salary, level):
        super().__init__(name, age, salary)
        self.level = level
    
    def work(self):
        print(f"{self.name} is coding")

class Designer(Employee):
    def work(self):
        print(f"{self.name} is drawing")
    

se1 = SoftwareEngineer("Max" ,25, 6000 , "Junior")
se1.work()

d = Designer("Philipp", 27, 7000)
d.work()

# Polymorphism
employees = [SoftwareEngineer("Max" ,25, 6000 , "Junior"),
            SoftwareEngineer("Max" ,30, 9000 , "Senior"),
            Designer("Philipp", 27, 7000)
            ]

def motivate_employees(employees):
    for employee in employees:
        employee.work()

motivate_employees(employees)

요약

  1. 상속 : ChildClass(BaseClass)
  2. 상속, 확장, 재정의
  3. super().init()()
  4. 다형성