Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Introduction to Objective-C Composition

Objective-C Basics - Composition Brainstorm

@ http://teamtreehouse.com/library/objectivec-basics/introduction-to-objectivec/composition

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?

1 Answer

Jiayi Liu
Jiayi Liu
12,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)