![Page 1: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/1.jpg)
Andrei Raifura iOS Department Manager, YOPESO
#CodeWăy
Inheritance - The myth of code reuse
![Page 2: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/2.jpg)
• How does Inheritance help us to reuse the code
• Why it doesn’t work
• The pitfalls of Inheritance
• To use or not to use
Agenda
![Page 3: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/3.jpg)
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
OOP principlesI will teach you OOP
![Page 4: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/4.jpg)
Inheritance
“…It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces…”
Wikipedia, Inheritance (object-oriented programming)
![Page 5: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/5.jpg)
Inheritance
‣ Two ears ‣ For legs ‣ Tail
Animal
![Page 6: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/6.jpg)
Case study
![Page 7: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/7.jpg)
Case study
CustomerYou
![Page 8: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/8.jpg)
Case study
Can you program a Toyota Corolla for me?
CustomerYou
![Page 9: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/9.jpg)
Case study
Mm..Sure!!
CustomerYou
![Page 10: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/10.jpg)
Toyota Corolla
Wheels
Manufacturer
Transmission
4
Toyota
Front-wheel drive
![Page 11: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/11.jpg)
Defining Car
class Car { let frontLeft = Wheel() let frontRight = Wheel() let rearLeft = Wheel() let rearRight = Wheel()
![Page 12: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/12.jpg)
Defining Car
class Car { let frontLeft = Wheel() let frontRight = Wheel() let rearLeft = Wheel() let rearRight = Wheel() var manufacturer: String { get { return "Undefined" } }
![Page 13: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/13.jpg)
Defining Car
class Car { . . .
func turnLeft(degrees: Double) { frontLeft.turnLeft(degrees) frontRight.turnLeft(degrees) } func turnRight(degrees: Double) { frontLeft.turnRight(degrees) frontRight.turnRight(degrees) }
![Page 14: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/14.jpg)
Defining Car
class Car { . . .
func accelerate(kph: Double) { frontLeft.rotate(kph) frontRight.rotate(kph) } }
![Page 15: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/15.jpg)
Defining Wheel
class Wheel { private var angle = 0.0 private var rotationSpeed = 0.0 func turnRight(degrees: Double) { angle += degrees } func turnLeft(degrees: Double) { angle -= degrees } func rotate(kph: Double) { rotationSpeed = kph } }
![Page 16: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/16.jpg)
Defining Toyota Corolla
class ToyotaCorolla: Car { override var manufacturer: String { get { return "Toyota" } } }
![Page 17: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/17.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
![Page 18: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/18.jpg)
Case study
Great! Now, i’d like to have a Toyota Corolla Sport
CustomerYou
![Page 19: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/19.jpg)
Case study
Ok! will do!
CustomerYou
![Page 20: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/20.jpg)
Toyota Corolla
Wheels
Manufacturer
Transmission
4
Toyota
Front-wheel drive
![Page 21: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/21.jpg)
Toyota Corolla
Wheels
Manufacturer
Transmission
4
Toyota
Front-wheel drive
![Page 22: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/22.jpg)
Toyota Corolla Sport
Wheels
Manufacturer
Transmission
4
Toyota
Rear-wheel drive
![Page 23: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/23.jpg)
Defining Toyota Corolla Sport
class ToyotaCorollaSport: ToyotaCorolla { override func accelerate(kph: Double) { rearLeft.rotate(kph) rearRight.rotate(kph) } }
![Page 24: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/24.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
![Page 25: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/25.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
![Page 26: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/26.jpg)
Case study
Woww! Now a Honda Civic and a Honda Civic
Sport
CustomerYou
![Page 27: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/27.jpg)
Case study
Mmnn.. ok
CustomerYou
![Page 28: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/28.jpg)
Honda Civic
Wheels
Manufacturer
Transmission
4
Front-wheel drive
Honda
![Page 29: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/29.jpg)
Honda Civic Sport
Wheels
Manufacturer
Transmission
4
Rear-wheel drive
Honda
![Page 30: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/30.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
![Page 31: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/31.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
HondaCivic
HondaCivicSport
![Page 32: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/32.jpg)
Defining Front-wheel Drive
class FrontWheelDriveCar: Car { override func accelerate(kph: Double) { frontLeft.rotate(kph) frontRight.rotate(kph) } }
![Page 33: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/33.jpg)
Defining Rear-wheel Drive
class RearWheelDriveCar: Car { override func accelerate(kph: Double) { rearLeft.rotate(kph) rearRight.rotate(kph) } }
![Page 34: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/34.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
HondaCivic
HondaCivicSport
![Page 35: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/35.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar
ToyotaCorollaSport
RearWheelDriveCar
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
HondaCivic HondaCivicSport
![Page 36: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/36.jpg)
Refactoring Toyota Corolla
class ToyotaCorolla: FrontWheelDriveCar { override var manufacturer: String { get { return "Toyota" } } }
![Page 37: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/37.jpg)
Refactoring Toyota Corolla Sport
class ToyotaCorollaSport: RearWheelDriveCar { override var manufacturer: String { get { return "Toyota" } } }
![Page 38: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/38.jpg)
Defining Honda Civic
class HondaCivic: FrontWheelDriveCar { override var manufacturer: String { get { return "Honda" } } }
![Page 39: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/39.jpg)
Defining Honda Civic Sport
class HondaCivicSport: RearWheelDriveCar { override var manufacturer: String { get { return "Honda" } } }
![Page 40: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/40.jpg)
Lexus GX
Wheels
Manufacturer
Transmission
4
All-wheel drive
Lexus
![Page 41: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/41.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar
ToyotaCorollaSport
RearWheelDriveCar
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
HondaCivic HondaCivicSport
![Page 42: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/42.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar
ToyotaCorollaSport
RearWheelDriveCar
ToyotaCorolla
- turnLeft - turnRight - rotate
Wheel
HondaCivic HondaCivicSport
AllWheelDriveCar
LexusGX
![Page 43: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/43.jpg)
Defining All-wheel Drive
class AllWheelDriveCar: Car { override func accelerate(kph: Double) { frontLeft.rotate(kph) frontRight.rotate(kph) rearLeft.rotate(kph) rearRight.rotate(kph) } }
![Page 44: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/44.jpg)
Defining Lexus GX
class LexusGX: AllWheelDriveCar { override var manufacturer: String { get { return "Lexus" } } }
![Page 45: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/45.jpg)
Case study
Amazing! But, can you make
me an experimental car?
CustomerYou
![Page 46: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/46.jpg)
Case study
It will switch between two-wheel drive and all-wheel
drive…
CustomerYou
![Page 47: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/47.jpg)
Case study
And will turn with all four wheels.
CustomerYou
![Page 48: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/48.jpg)
Experimental
Wheels
Manufacturer
Transmission
4
Front-wheel drive
Experimental
![Page 49: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/49.jpg)
Experimental
Wheels
Manufacturer
Transmission
4
Front-wheel drive & All-wheel drive
Experimental
![Page 50: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/50.jpg)
Experimental
Wheels
Manufacturer
Transmission
4
Front-wheel drive & All-wheel drive
Experimental
Steering All-wheel steering
![Page 51: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/51.jpg)
Are you Seriously?
![Page 52: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/52.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft - turnRight - rotate
Wheel
AllWheelDriveCar
![Page 53: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/53.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft - turnRight - rotate
Wheel
AllWheelDriveCar
ExperimentalCar
![Page 54: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/54.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft - turnRight - rotate
Wheel
AllWheelDriveCar
ExperimentalCar
![Page 55: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/55.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft - turnRight - rotate
Wheel
AllWheelDriveCar
ExperimentalCar
![Page 56: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/56.jpg)
Class Diagram
- manufacturer - turnLeft - turnRight - accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft - turnRight - rotate
Wheel
AllWheelDriveCar
ExperimentalCar
The Diamond of Dread
![Page 57: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/57.jpg)
Code reuse
“…It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces…”
Wikipedia, Inheritance (object-oriented programming)
![Page 58: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/58.jpg)
Code reuse
“…It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces…”
Wikipedia, Inheritance (object-oriented programming)
![Page 59: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/59.jpg)
Solutions
![Page 60: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/60.jpg)
Demo
![Page 61: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/61.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Makes the code fragile
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
![Page 62: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/62.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Makes the code fragile
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
![Page 63: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/63.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
• Makes the code fragile
![Page 64: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/64.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
• Makes the code fragile
![Page 65: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/65.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
• Makes the code fragile
![Page 66: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/66.jpg)
Liskov Substitution Principle
Subtypes must be substitutable for their base types.
Robert C. Martin (Uncle Bob)
![Page 67: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/67.jpg)
Violation of LSP
class File { var path: String! var data: NSData! func loadData() { // Retrieve data from disk } func saveData() { // Write data to disk } }
![Page 68: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/68.jpg)
class ReadOnlyFile: File { override func saveData() { print("Cannot write data") } }
Violation of LSP
![Page 69: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/69.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
• Makes the code fragile
![Page 70: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/70.jpg)
Inheritance pitfalls
• A very tight binding between a superclass and its subclasses
• Makes the code fragile
• Difficult to debug
• Inheritance relationships generally can't be altered at runtime.
• Leads to violation of Liskov Substitution Principle
• Difficult to test
![Page 71: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/71.jpg)
Should we avoid inheritance altogether?
![Page 72: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/72.jpg)
To use?
Use Inheritance when your derived class truly is the type you're extending.And it will always be!
![Page 73: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/73.jpg)
The Employee
- firstName - lastName - age - department
Employee
![Page 74: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/74.jpg)
The Employee and the Student
- firstName - lastName - age - year - faculty
Student
- firstName - lastName - age - department
Employee
![Page 75: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/75.jpg)
The Employee and the Student
- firstName - lastName - age
Person
- department
Employee
- year - faculty
Student
![Page 76: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/76.jpg)
The Employee and the Student
- firstName - lastName - age
Person
- department
Employee
- year - faculty
Student
I am a Student. And an Employee.
![Page 77: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/77.jpg)
The Employee and the Student
- firstName - lastName - age
Person
- department
Employee
- year - faculty
Student
I am a Student. And an Employee.
I've just become unemployed.
![Page 78: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/78.jpg)
The Employee and the Student
- firstName - lastName - age
Person
Occupation
- department
Employee- year - faculty
Student Unemployed
![Page 79: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/79.jpg)
Or not to use?
How about polymorphism?
Consider using Protocols* to achieve a polymorphic behaviour.* Also known as Interfaces in other languages
![Page 80: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/80.jpg)
I need to reuse some code from superclass. No
The derived class is almost the extending type No
The derived class is the extending type but might change in the future No
I need a polymorphic behaviour No
The derived class truly is the extending type. And it won't change. I swear!
Yes
To use or Not to use?
![Page 81: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/81.jpg)
• How does Inheritance help us to reuse the code
• Why it doesn’t work
• The pitfalls of Inheritance
• To use or not to use
Review
![Page 82: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/82.jpg)
Use Inheritance Wisely!
![Page 83: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/83.jpg)
Questions?
![Page 84: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/84.jpg)
Contacts
Raifura Andrei iOS Department Manager, YOPESO
#CodeWăy
![Page 85: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015](https://reader031.vdocuments.mx/reader031/viewer/2022030123/58a426541a28abec1a8b6d3d/html5/thumbnails/85.jpg)