SlideShare a Scribd company logo
Object Oriented Programming
in Swift
Ch 1: Inheritance
Sai Li @ Yowoo Tech.
2016/12/5
References
‱ Agile Software Development: Principles, Patterns,
and Practice ( : )‹
by Robert C. Martin
‱ Fundamental Object Oriented Programming ‹
by Hsuan-Tien Lin
‱ ‹
by Teddy Chen
‱ Essential Object-Oriented Programming‹
by Josh Ko
OOP
‱ Encapsulation
‱ Inheritance
‱ Polymorphism
Spaghetti
v.s
Ravioli
Inheritance
Has-A
‱ Has-A: a basic way to reuse variables/methods
in other classes
Has-A
‱ Has-A: a basic way to deïŹne components of a system
‱ iPhone has a camera
Has-A
‱ Has-A: a basic mechanism for
objects to connect with each other
‱ He has 2 friends: Tom and Jerry
Has-A: most basic design component of OOP
chi
‱ Admin is a user
‱ can be implemented via has-a:‹
Everyone has Brokeback Mountain in mind
child
‱ Type A inherit TypeB: TypeA is a (special case of) TypeB
‱ TypeSubClass (TypeDerivedClass or ChildClass) inherit‹
TypeSuperClass (TypeBaseClass or ParentClass)
is-a != has-a
Uses of Is-A
‱ CDAndCassetePlayer is a CassettePlayer
Uses of Is-A
‱ CassettePlayer is a (concrete type of) Player
‱ CDPlayer is a (concrete type of) Player
Uses of Is-A
‱ CDplayer is a (update of) CassettePlayer
‱ some behaviors “changed”
‱ no as clear as the previous case, but a potential trick
in OOP
‱ Multiple inheritance: Not supported in Swift
‱ Swift: single inheritance (Java)
Diamond Problem
?
Diamond Problem
Is-A
‱ is an extended type of‹
- FunPerson is Person who can tell jokes
‱ is a more concrete/restricted description of ‹
- YoungPerson is Person who is young
‱ (is an update of)‹
- NewStaff replaces ExistingStaff
Object Oriented Programming in Swift Ch1 - Inheritance
‱ Over-use of inherit for is-a: 1 class -> 1 instance
‱ Usual goal of OOP: one class, many instances
Is-A
‱ Under-user of inherit for is-a: overly complicated class
YowooStaff
‱ Bugs/hacks?‹
cali = YowooStaff(id: “”, name: “CYLi”, title: “CFO”)
Terminology
Instance Variables and Inheritance
‱ Staff: 1 instance variables
‱ YowooStaff: 2 instance variables
Instance Variables and Inheritance
‱ instance variable binding: determined at
compile time
‱ same “name” can NOT co-exist in a class,
binding determined by compile-time type.
Instance Methods and Inheritance
‱ Staff: 1 instance method
‱ RD: 2 instance methods
‱ what output?
‱ instance method binding: dynamic, depending
on run-time instance types
Reference Assignment
‱ saiLi is an instance of GoodStudent
‱ saiLi is an instance of Student, too
‱ one instance, many coherent types
Constructor and Inheritance
‱ initialize self ïŹrst, ancestor last
‱ the GoodStudent part of the memory initialized ïŹrst,‹
the Student part
Swift Constructor Calls
Designate
Convenience
Swift Constructor Calls
Parent
Child
Private Variables and Inheritance
private variables are still “inherited” in
memory, but not “visible” to the subclass
because of encapsulation (security)
‱ protected: accessible to Child(subclass)
‱ public: everyone
‱ internal (default): same package classes
‱ protected: sub-classes
‱ private: class self
Swift Access Permissions
Access Permission (Swift)
‱ Child: same or more close than Parent
References
‱ Apple: Access Control and protected
‱ http://commons.oreilly.com/wiki/images/a/a7/
Beyond_Java_I_2_tt13.png
‱ https://upload.wikimedia.org/wikipedia/commons/
thumb/4/47/C3_linearization_example.svg/600px-
C3_linearization_example.svg.png
‱ https://developer.apple.com/library/content/
documentation/Swift/Conceptual/
Swift_Programming_Language/Art/
initializerDelegation02_2x.png

More Related Content

KEY
OOP in JS
PPTX
2 Day Android Workshop
PDF
Object Oriented Programming in Swift Ch0 - Encapsulation
PPTX
IFI7184.DT lesson1- Programming languages
PPT
RIBBUN SOFTWARE
PPT
Introduction what is java
PDF
Object Oriented Programming Ch3 SRP, DIP, ISP
PDF
Shamaeva fearless sourcing on LinkedIn 19.2.2014
OOP in JS
2 Day Android Workshop
Object Oriented Programming in Swift Ch0 - Encapsulation
IFI7184.DT lesson1- Programming languages
RIBBUN SOFTWARE
Introduction what is java
Object Oriented Programming Ch3 SRP, DIP, ISP
Shamaeva fearless sourcing on LinkedIn 19.2.2014

Similar to Object Oriented Programming in Swift Ch1 - Inheritance (20)

DOC
Research paper
PDF
Unit 3 notes.pdf
PPTX
Inheritance in Python Inheritance in Pyt
PPTX
OOP lecture 04
PPTX
O op lecture 04
PDF
Java programming -Object-Oriented Thinking- Inheritance
PDF
Inheritance and Substitution
PPTX
Inheritance in Object-Oriented Programming (OOP) (not).pptx
PPTX
Inheritance in oops
PDF
java_vyshali.pdf
PPT
Overview of Object Oriented Programming using C++
PDF
Swift Tutorial Part 2. The complete guide for Swift programming language
PPTX
Relationship of inheritance in oopm.pptx
PDF
Object Oriented Programming
PDF
lecture 6.pdf
PPTX
Lecture 3
PPT
presentation on inheritance concept in c++
PPTX
Inheritance in OOPs with java
PPTX
Swift vs Objective-C
PPTX
java part 1 computer science.pptx
Research paper
Unit 3 notes.pdf
Inheritance in Python Inheritance in Pyt
OOP lecture 04
O op lecture 04
Java programming -Object-Oriented Thinking- Inheritance
Inheritance and Substitution
Inheritance in Object-Oriented Programming (OOP) (not).pptx
Inheritance in oops
java_vyshali.pdf
Overview of Object Oriented Programming using C++
Swift Tutorial Part 2. The complete guide for Swift programming language
Relationship of inheritance in oopm.pptx
Object Oriented Programming
lecture 6.pdf
Lecture 3
presentation on inheritance concept in c++
Inheritance in OOPs with java
Swift vs Objective-C
java part 1 computer science.pptx
Ad

More from Chihyang Li (20)

PDF
Image Style Transfer and AI on iOS Mobile App
PDF
Image style transfer and iOS CoreML, Vision Frameworks
PDF
Machine Learning in Swift
PDF
Image style transfer & AI on App
PDF
iOS Vision framework
PDF
Design Patterns in Swift Ch5 Abstract Factory Builder
PDF
Design patterns in Swift Ch4: Iterator & Template Method
PDF
Swift map & flatMap
PDF
Swift Optional ??
PDF
Design Pattern in Swift Ch2 Strategy, Decorator
PDF
Design Pattern Ch3 in Swift Composite
PDF
Design Patterns in Swift Ch1 Factory Method
PDF
Scrum & Kanban Introduction
PDF
Design Patterns in Swift ch0 Introduction
PDF
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
PDF
Apple Swift API Design Guideline
PDF
Swift capture
PDF
DevOps 2016 summit
PDF
Slack introduction
PDF
Agile Development: Scrum introduction
Image Style Transfer and AI on iOS Mobile App
Image style transfer and iOS CoreML, Vision Frameworks
Machine Learning in Swift
Image style transfer & AI on App
iOS Vision framework
Design Patterns in Swift Ch5 Abstract Factory Builder
Design patterns in Swift Ch4: Iterator & Template Method
Swift map & flatMap
Swift Optional ??
Design Pattern in Swift Ch2 Strategy, Decorator
Design Pattern Ch3 in Swift Composite
Design Patterns in Swift Ch1 Factory Method
Scrum & Kanban Introduction
Design Patterns in Swift ch0 Introduction
Object Oriented Programming in Swift Ch2 Polymorphism, OCP, LSP
Apple Swift API Design Guideline
Swift capture
DevOps 2016 summit
Slack introduction
Agile Development: Scrum introduction
Ad

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
top salesforce developer skills in 2025.pdf
PPT
Introduction Database Management System for Course Database
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
System and Network Administration Chapter 2
PDF
AI in Product Development-omnex systems
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Nekopoi APK 2025 free lastest update
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Wondershare Filmora 15 Crack With Activation Key [2025
top salesforce developer skills in 2025.pdf
Introduction Database Management System for Course Database
2025 Textile ERP Trends: SAP, Odoo & Oracle
Adobe Illustrator 28.6 Crack My Vision of Vector Design
System and Network Administration Chapter 2
AI in Product Development-omnex systems
L1 - Introduction to python Backend.pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Understanding Forklifts - TECH EHS Solution
CHAPTER 2 - PM Management and IT Context
Nekopoi APK 2025 free lastest update
Softaken Excel to vCard Converter Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
How Creative Agencies Leverage Project Management Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

Object Oriented Programming in Swift Ch1 - Inheritance