SlideShare une entreprise Scribd logo
1  sur  85
Télécharger pour lire hors ligne
Andrei Raifura iOS Department Manager, YOPESO
#CodeWăy
Inheritance -
The myth of code reuse
• 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
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
OOP principles
I will teach
you OOP
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)
Inheritance
‣ Two ears
‣ For legs
‣ Tail
Animal
Case study
Case study
CustomerYou
Case study
Can you program a
Toyota Corolla for me?
CustomerYou
Case study
Mm..Sure!!
CustomerYou
Toyota Corolla
Wheels
Manufacturer
Transmission
4
Toyota
Front-wheel drive
Defining Car
class Car {
let frontLeft = Wheel()
let frontRight = Wheel()
let rearLeft = Wheel()
let rearRight = Wheel()
Defining Car
class Car {
let frontLeft = Wheel()
let frontRight = Wheel()
let rearLeft = Wheel()
let rearRight = Wheel()
var manufacturer: String {
get { return "Undefined" }
}
Defining Car
class Car {
. . .
func turnLeft(degrees: Double) {
frontLeft.turnLeft(degrees)
frontRight.turnLeft(degrees)
}
func turnRight(degrees: Double) {
frontLeft.turnRight(degrees)
frontRight.turnRight(degrees)
}
Defining Car
class Car {
. . .
func accelerate(kph: Double) {
frontLeft.rotate(kph)
frontRight.rotate(kph)
}
}
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
}
}
Defining Toyota Corolla
class ToyotaCorolla: Car {
override var manufacturer: String {
get { return "Toyota" }
}
}
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
Case study
Great!
Now, i’d like to have a
Toyota Corolla Sport
CustomerYou
Case study
Ok! will do!
CustomerYou
Toyota Corolla
Wheels
Manufacturer
Transmission
4
Toyota
Front-wheel drive
Toyota Corolla
Wheels
Manufacturer
Transmission
4
Toyota
Front-wheel drive
Toyota Corolla Sport
Wheels
Manufacturer
Transmission
4
Toyota
Rear-wheel drive
Defining Toyota Corolla Sport
class ToyotaCorollaSport: ToyotaCorolla {
override func accelerate(kph: Double) {
rearLeft.rotate(kph)
rearRight.rotate(kph)
}
}
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
Case study
Woww!
Now a Honda Civic
and a Honda Civic
Sport
CustomerYou
Case study
Mmnn.. ok
CustomerYou
Honda Civic
Wheels
Manufacturer
Transmission
4
Front-wheel drive
Honda
Honda Civic Sport
Wheels
Manufacturer
Transmission
4
Rear-wheel drive
Honda
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
HondaCivic
HondaCivicSport
Defining Front-wheel Drive
class FrontWheelDriveCar: Car {
override func accelerate(kph: Double) {
frontLeft.rotate(kph)
frontRight.rotate(kph)
}
}
Defining Rear-wheel Drive
class RearWheelDriveCar: Car {
override func accelerate(kph: Double) {
rearLeft.rotate(kph)
rearRight.rotate(kph)
}
}
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
ToyotaCorollaSport
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
HondaCivic
HondaCivicSport
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar
ToyotaCorollaSport
RearWheelDriveCar
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
HondaCivic HondaCivicSport
Refactoring Toyota Corolla
class ToyotaCorolla: FrontWheelDriveCar {
override var manufacturer: String {
get { return "Toyota" }
}
}
Refactoring Toyota Corolla Sport
class ToyotaCorollaSport: RearWheelDriveCar {
override var manufacturer: String {
get { return "Toyota" }
}
}
Defining Honda Civic
class HondaCivic: FrontWheelDriveCar {
override var manufacturer: String {
get { return "Honda" }
}
}
Defining Honda Civic Sport
class HondaCivicSport: RearWheelDriveCar {
override var manufacturer: String {
get { return "Honda" }
}
}
Lexus GX
Wheels
Manufacturer
Transmission
4
All-wheel drive
Lexus
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar
ToyotaCorollaSport
RearWheelDriveCar
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
HondaCivic HondaCivicSport
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar
ToyotaCorollaSport
RearWheelDriveCar
ToyotaCorolla
- turnLeft
- turnRight
- rotate
Wheel
HondaCivic HondaCivicSport
AllWheelDriveCar
LexusGX
Defining All-wheel Drive
class AllWheelDriveCar: Car {
override func accelerate(kph: Double) {
frontLeft.rotate(kph)
frontRight.rotate(kph)
rearLeft.rotate(kph)
rearRight.rotate(kph)
}
}
Defining Lexus GX
class LexusGX: AllWheelDriveCar {
override var manufacturer: String {
get { return "Lexus" }
}
}
Case study
Amazing!
But, can you make
me an experimental
car?
CustomerYou
Case study
It will switch
between two-wheel
drive and all-wheel
drive…
CustomerYou
Case study
And will turn with all
four wheels.
CustomerYou
Experimental
Wheels
Manufacturer
Transmission
4
Front-wheel drive
Experimental
Experimental
Wheels
Manufacturer
Transmission
4
Front-wheel drive &
All-wheel drive
Experimental
Experimental
Wheels
Manufacturer
Transmission
4
Front-wheel drive &
All-wheel drive
Experimental
Steering All-wheel steering
Are you Seriously?
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft
- turnRight
- rotate
Wheel
AllWheelDriveCar
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft
- turnRight
- rotate
Wheel
AllWheelDriveCar
ExperimentalCar
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft
- turnRight
- rotate
Wheel
AllWheelDriveCar
ExperimentalCar
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft
- turnRight
- rotate
Wheel
AllWheelDriveCar
ExperimentalCar
Class Diagram
- manufacturer
- turnLeft
- turnRight
- accelerate
Car
FrontWheelDriveCar RearWheelDriveCar
- turnLeft
- turnRight
- rotate
Wheel
AllWheelDriveCar
ExperimentalCar
The Diamond of Dread
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)
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)
Solutions
Demo
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
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
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
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
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
Liskov Substitution Principle
Subtypes must be substitutable for their base types.
Robert C. Martin (Uncle Bob)
Violation of LSP
class File {
var path: String!
var data: NSData!
func loadData() {
// Retrieve data from disk
}
func saveData() {
// Write data to disk
}
}
class ReadOnlyFile: File {
override func saveData() {
print("Cannot write data")
}
}
Violation of LSP
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
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
Should we avoid inheritance
altogether?
To use?
Use Inheritance when your derived class truly is the type you're extending.
And it will always be!
The Employee
- firstName
- lastName
- age
- department
Employee
The Employee and the Student
- firstName
- lastName
- age
- year
- faculty
Student
- firstName
- lastName
- age
- department
Employee
The Employee and the Student
- firstName
- lastName
- age
Person
- department
Employee
- year
- faculty
Student
The Employee and the Student
- firstName
- lastName
- age
Person
- department
Employee
- year
- faculty
Student
I am a Student.
And an Employee.
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.
The Employee and the Student
- firstName
- lastName
- age
Person
Occupation
- department
Employee
- year
- faculty
Student Unemployed
Or not to use?
How about polymorphism?
Consider using Protocols* to achieve a polymorphic behaviour.
* Also known as Interfaces in other languages
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?
• 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
Use Inheritance Wisely!
Questions?
Contacts
Raifura Andrei
iOS Department Manager, YOPESO
andrei.raifura@yopeso.com
thelvis4@gmail.com
#CodeWăy
Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Contenu connexe

Similaire à Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

QCon New York - Migrating to Cloud Native with Microservices
QCon New York - Migrating to Cloud Native with MicroservicesQCon New York - Migrating to Cloud Native with Microservices
QCon New York - Migrating to Cloud Native with Microservices
Adrian Cockcroft
 

Similaire à Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015 (20)

Reactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDaysReactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDays
 
David Bilík: Anko – modern way to build your layouts?
David Bilík: Anko – modern way to build your layouts?David Bilík: Anko – modern way to build your layouts?
David Bilík: Anko – modern way to build your layouts?
 
From V8 to Modern Compilers
From V8 to Modern CompilersFrom V8 to Modern Compilers
From V8 to Modern Compilers
 
Webinar: From Relational Databases to MongoDB - What You Need to Know
Webinar: From Relational Databases to MongoDB - What You Need to KnowWebinar: From Relational Databases to MongoDB - What You Need to Know
Webinar: From Relational Databases to MongoDB - What You Need to Know
 
Windycityrails page performance
Windycityrails page performanceWindycityrails page performance
Windycityrails page performance
 
The Ember.js Framework - Everything You Need To Know
The Ember.js Framework - Everything You Need To KnowThe Ember.js Framework - Everything You Need To Know
The Ember.js Framework - Everything You Need To Know
 
MySQL Developer Day conference: MySQL Replication and Scalability
MySQL Developer Day conference: MySQL Replication and ScalabilityMySQL Developer Day conference: MySQL Replication and Scalability
MySQL Developer Day conference: MySQL Replication and Scalability
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
 
Anti Object-Oriented Design Patterns
Anti Object-Oriented Design PatternsAnti Object-Oriented Design Patterns
Anti Object-Oriented Design Patterns
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
 
The State of Wicket
The State of WicketThe State of Wicket
The State of Wicket
 
Chaos Testing with F# and Azure by Rachel Reese at Codemotion Dubai
Chaos Testing with F# and Azure by Rachel Reese at Codemotion DubaiChaos Testing with F# and Azure by Rachel Reese at Codemotion Dubai
Chaos Testing with F# and Azure by Rachel Reese at Codemotion Dubai
 
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
 
Tom Kyte at Hotsos 2015
Tom Kyte at Hotsos 2015Tom Kyte at Hotsos 2015
Tom Kyte at Hotsos 2015
 
The Java Carputer
The Java CarputerThe Java Carputer
The Java Carputer
 
QCon New York - Migrating to Cloud Native with Microservices
QCon New York - Migrating to Cloud Native with MicroservicesQCon New York - Migrating to Cloud Native with Microservices
QCon New York - Migrating to Cloud Native with Microservices
 
Configuration for Java EE and the Cloud
Configuration for Java EE and the CloudConfiguration for Java EE and the Cloud
Configuration for Java EE and the Cloud
 
Svelte the future of frontend development
Svelte   the future of frontend developmentSvelte   the future of frontend development
Svelte the future of frontend development
 
“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core
 
Web servicesoverview
Web servicesoverviewWeb servicesoverview
Web servicesoverview
 

Dernier

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Dernier (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015