Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Objective-C Basics - Composition Brainstorm
I just want to brainstorm what I've learned in this video and would like to know if there's anything I'm missing.
*The purpose of composition is to create an object that's composed of other objects.
1) a subclass of NSObject is created called "Button". 2) this subclass has an @property called "shape" with a type Shape *. 3) in the main file, to create a square button, we first create an instance of the class Rectangle called "square", which inherits from Shape. 4) then we create an instance of the class Button. 5) refer to the "square" shape for the Button's shape. 6) now this Button object has a property of square shape.
Ok, so I get all that, but what's polymorphism?
Jiayi Liu12,199 Points
poly := ">1"; morphism := "how something is formed";
There are plenty of ways to realise polymorphism: composition, inheritance, interfaces(as far I learned);
Examples in Programmatic Biology: Composition: You bought a car, by which you obtained a @property (weak,nonatomic) Speed (*MaxSpeedOfVehicle) , which does not belongs to you, but you obtained the speed anyway, while you may also choose to move at your own speed;
Inheritance: You now become a turtle, by which you inherited a @property (strong,nonatomic) Speed *maxSpeedOfTurtle;
Interface: you are a human with both your legs cut off, but your parents let you implement the WheelChair interface so, although every human has his @property (strong,nonatomic) Speed *maxSpeedOfHuman, whenever you try to move, your have to resort the overridden [me move] function with *maxSpeedOfWheelChair property;
Yet of course you may become a turtle with your four-legs cut off and buy a turtle car together with a TurtleWheelChair, that's how polymorphism works.
In short fashion, polymorphism is to make object obtain properties of other Species. (maybe)