1. Inheritance πŸ‘©β€πŸ’»

1. Defining a Base Class

λ‹€λ₯Έ Classes λ₯Ό μƒμ†ν•˜μ§€ μ•Šμ€ Class λ₯Ό Base Class라 ν•œλ‹€.

class Vehicle {
    var currentSpeed = 0.0
    var description: String {
        return "traveling at \(currentSpeed) miles per hour"
    }
    func makeNoise() {
        // do nothing - an arbitrary vehicle doesn't necessarily make a noise
    }
}

Universal Base Classλ₯Ό ν•˜λ‚˜ λ§Œλ“€μ–΄ λͺ¨λ“  Classes κ°€ 기본적으둜 이것을 μƒμ†ν•˜λ„λ‘ ν•˜λŠ” 일뢀 언어와 달리 Swift λŠ” λͺ…μ‹œμ μœΌλ‘œ 상속을 ν•˜μ§€ μ•Šμ€ Class λŠ” buildν•  λ•Œ μžλ™μœΌλ‘œ Base Classκ°€ λœλ‹€.

2. Subclassing

Subclassing은 μ‘΄μž¬ν•˜λŠ” λ‹€λ₯Έ Class λ₯Ό 기반으둜 new Class λ₯Ό μƒμ„±ν•˜λŠ” ν–‰μœ„λ₯Ό λ§ν•œλ‹€.
기쑴의 Class λ₯Ό Superclass, 기쑴의 Class λ₯Ό 상속해 μƒˆλ‘œ μƒμ„±λœ Class λ₯Ό Subclass라 ν•˜λ©°, Subclass λŠ” μƒˆλ‘œμš΄ 것을 μΆ”κ°€ν•˜λŠ” 것은 물둠이고, 기쑴의 것을 μˆ˜μ •ν•  수 μžˆλ‹€.

Subclassing 은 :을 μ΄μš©ν•΄ μƒμ„±ν•œλ‹€.

Syntax

class SomeSubclass: SomeSuperclass {
    // subclass definition goes here
}


μœ„ Vehicle Class λ₯Ό μƒμ†ν•œ Bicycle Class λ₯Ό λ§Œλ“€μ–΄λ³΄μž.

class Bicycle: Vehicle {
    var hasBasket = false
}
  • Vehicle 은 Base Classμ΄λ©΄μ„œ, Bicycle 의 Superclassλ‹€.
  • Bicycle 은 Vehicle 의 Subclass둜, Vehicle 의 λͺ¨λ“  characteristicsλ₯Ό μƒμ†ν•œλ‹€.
let bicycle = Bicycle()
bicycle.hasBasket = true
bicycle.currentSpeed = 15.0
print("Bicycle: \(bicycle.description)") // Bicycle: traveling at 15.0 miles per hour


그리고 Subclass λŠ” 또 λ‹€λ₯Έ Subclassing 될 수 μžˆλ‹€.

class Tandem: Bicycle {
    var currentNumberOfPassengers = 0
}
  • Bicycle 은 Vehicle 의 Subclassμ΄λ©΄μ„œ Tandem 의 Superclassλ‹€.
  • Tandem 은 Bicycle 의 Subclass둜, Bicycle 의 λͺ¨λ“  characteristicsλ₯Ό μƒμ†ν•˜λ―€λ‘œ Vehicle 의 λͺ¨λ“  characteristics λ˜ν•œ μžλ™μœΌλ‘œ μƒμ†ν•œλ‹€.
let tandem = Tandem()
tandem.hasBasket = true
tandem.currentNumberOfPassengers = 2
tandem.currentSpeed = 22.0
print("Tandem: \(tandem.description)")  // Tandem: traveling at 22.0 miles per hour

2. Overriding πŸ‘©β€πŸ’»

1. Overriding

Subclass λŠ” Superclass 의 Instance Methods, Type Methods, Instance Properties, Type Properties, Subscripts λ₯Ό λ‹€μ‹œ κ΅¬ν˜„ν•  수 μžˆλ‹€. 이것을 Overriding이라 ν•œλ‹€.

Overriding 을 μœ„ν•΄μ„œ μ•žμ— override modifier λ₯Ό λΆ™μ—¬μ€€λ‹€.
μ΄λ ‡κ²Œ ν•˜λŠ” 것은 μž¬μ •μ˜λ₯Ό λͺ…ν™•νžˆ ν•˜κ³ , μ‹€μˆ˜λ‘œ μž¬μ •μ˜ν•˜λŠ” 것을 λ°©μ§€ν•˜κΈ° μœ„ν•œ κ²ƒμœΌλ‘œ, override modifier 없이 μž¬μ •μ˜ν•˜λ©΄ Swift λŠ” 이λ₯Ό ν™•μΈν•˜κ³  compile errorλ₯Ό λ°œμƒμ‹œν‚¨λ‹€.

Overriding κ°€λŠ₯ν•œ characteristicsλŠ” mutableν•œ κ²ƒμœΌλ‘œ μ œν•œλœλ‹€. 예λ₯Ό λ“€μ–΄ let ν‚€μ›Œλ“œλ‘œ μ„ μ–Έλœ 경우 immutable이기 λ•Œλ¬Έμ— Overriding ν•  수 μ—†λ‹€.

2. Accessing Superclass Methods, Properties, and Subscripts

λ•Œλ‘œλŠ” Superclass 에 μ ‘κ·Όν•˜λŠ” 것이 μœ μš©ν•  λ•Œκ°€ 있으며, super ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄ μ ‘κ·Όν•  수 μžˆλ‹€. λ‹€μŒμ€ Overriding 된 characteristics의 μΌ€μ΄μŠ€λ³„ super μ ‘κ·Ό μ˜ˆμ‹œλ‹€.

  • someMethod() : super.someMethod() λ₯Ό ν˜ΈμΆœν•œλ‹€.
  • someProperty : getter, setter λ₯Ό μ΄μš©ν•΄ super.Property 에 μ ‘κ·Όν•œλ‹€.
  • someIndex: super[someIndex] 둜 μ ‘κ·Όν•œλ‹€.
class TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int { multiplier * index }
    func printMultiplier() {
        print(multiplier)
    }
    init(multiplier: Int) {
        self.multiplier = multiplier
    }
}

class ArithmeticSequenceTable: TimesTable {
    var superMultiplier: Int { super.multiplier }
    override func printMultiplier() {
        super.printMultiplier()
    }
    override subscript(index: Int) -> Int { super[index] + 1 }
}

superλ₯Ό μ΄μš©ν•œ 접근은 Subclass λ‚΄μ—μ„œ μ ‘κ·Όν•˜λŠ” 것이닀. Subclass 의 Instance λ₯Ό 톡해 μ ‘κ·Όν•˜λŠ” 것이 μ•„λ‹ˆλ‹€.

3. Overriding Methods

class Train: Vehicle {
    override func makeNoise() {
        print("칙칙폭폭")
    }
}
let train = Train()
train.makeNoise()   // 칙칙폭폭

4. Overriding Properties

1 ) Overriding Property Getters and Setters

class Car: Vehicle {
    var gear = 1
    override var description: String {
        super.description + " in gear \(gear)"
    }
}
let car = Car()
car.currentSpeed = 25.0
car.gear = 3
print("Car: \(car.description)") // Car: traveling at 25.0 miles per hour in gear 3


2 ) Overriding Property Observers

class AutomaticCar: Car {
    override var currentSpeed: Double {
        didSet {
            gear = Int(currentSpeed / 10.0) + 1
        }
    }
}
let automatic = AutomaticCar()
automatic.currentSpeed = 35.0
print("AutomaticCar: \(automatic.description)") // AutomaticCar: traveling at 35.0 miles per hour in gear 4


3 ) Overriding Stored Properties

Stored Propertiesλ₯Ό Overriding ν•˜λŠ” 것은 λ‹€λ₯Έ Properties μ™€λŠ” 쑰금 λ‹€λ₯΄κΈ° λ•Œλ¬Έμ— 별도 ν¬μŠ€νŒ… Overriding Stored Properties 으둜 μž‘μ„±ν•œλ‹€.


3. Preventing Overrides πŸ‘©β€πŸ’»

Overriding을 막기 μœ„ν•΄ final modifier λ₯Ό μΆ”κ°€ν•  수 μžˆλ‹€. λ§Œμ•½ Subclass μ—μ„œ μž¬μ •μ˜ ν•  경우 Swift λŠ” 이λ₯Ό ν™•μΈν•˜κ³  compile errorλ₯Ό λ°œμƒμ‹œν‚¨λ‹€.

class AutomaticCar: Car {
    override final var currentSpeed: Double {
        didSet {
            gear = Int(currentSpeed / 10.0) + 1
        }
    }
}
class ElectricMotorCar: AutomaticCar {
    override var currentSpeed: Double { // error: Property overrides a 'final' property
        
    }
}

AutomaticCar 의 currentSpeed λ₯Ό Overriding ν•˜λ©΄μ„œ final modifier λ₯Ό λΆ™μ—¬μ£Όμ—ˆκΈ° λ•Œλ¬Έμ— AutomaticCar λ₯Ό μƒμ†ν•œ ElectricMotorCar λŠ” 이것을 μž¬μ •μ˜ ν•  수 μ—†λ‹€.

Properties, Methods, Subscripts κ°€ μ•„λ‹Œ Classes μ •μ˜μ— final modifier λ₯Ό μž‘μ„±ν•  경우, 이 Class λ₯Ό Subclassing ν•˜λ €λŠ” λͺ¨λ“  μ‹œλ„λŠ” compile-time error κ°€ λ°œμƒν•œλ‹€.




Reference

  1. β€œInheritance.” The Swift Programming Language Swift 5.7. accessed Nov. 29, 2022, Swift Docs Chapter 12 - Inheritance.