際際滷

際際滷Share 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

Object Oriented Programming in Swift Ch1 - Inheritance