Post on 16-Apr-2017
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 Programmingby Josh Ko
Has-A
• Has-A: a basic mechanism for objects to connect with each other
• He has 2 friends: Tom and Jerry
• Type A inherit TypeB: TypeA is a (special case of) TypeB • TypeSubClass (TypeDerivedClass or ChildClass) inherit
TypeSuperClass (TypeBaseClass or ParentClass)
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
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
• 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”)
• instance variable binding: determined at compile time
• same “name” can NOT co-exist in a class, binding determined by compile-time type.
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 first, ancestor last• the GoodStudent part of the memory initialized first,
the Student part
private variables are still “inherited” in memory, but not “visible” to the subclass
because of encapsulation (security)
• public: everyone
• internal (default): same package classes
• protected: sub-classes
• private: class self
Swift Access Permissions
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