Browse Questions
- - Inheritance — a class derives behaviour from a parent class ("is-a" relationship) - Composition — a type gets behaviour by holding references to other objects ("has-a" relationship) Swift…
- The core difference is value type vs reference type.
- A signature proves who sent a payload but says nothing about when, so an attacker who captures a valid signed request can resend it verbatim and it will still verify.
- Clean Architecture organises code into concentric layers where dependencies only point inward.
Answer:
- Inheritance — a class derives behaviour from a parent class ("is-a" relationship)
- Composition — a type gets behaviour by holding references to other objects ("has-a" relationship)
Swift favours composition through protocols and protocol extensions.
Code Example:
// ❌ Inheritance — rigid, can't mix and match
class Animal {
func breathe() { }
}
class Dog: Animal {
func bark() { }
}
class FlyingDog: Dog { } // gets everything even if unneeded
// ✅ Composition — flexible, modular
protocol Breathable { func breathe() }
protocol Swimmable { func swim() }
protocol Flyable { func fly() }
struct Duck: Breathable, Swimmable, Flyable {
func breathe() { }
func swim() { }
func fly() { }
}
// Protocol extensions provide default implementations
extension Swimmable {
func swim() { print("Splashing...") }
}