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 trialAlex Cevallos
16,551 Points#import vs subclassing with :
What is the exact difference between using the #import statement vs writing the subclass after the : (example is Shape : NSObject).
Don't they both serve to just subclass? Thanks
1 Answer
Sam Chaudry
25,519 PointsImporting just tells the compiler you are going to use the package/class in your code. So if you want to use properties defined somewhere else in the app you can import them in.
We subclass in order to specialise a type--to implement or change the behaviours or certain methods. The main purpose of inheritance is not code sharing.
For example if you have a UIColor object in my code [UIColor setColorToSamsColour: ];
I would create a subclass of UIColor called 'sam' with the properties I want so this could be say a special type of blue for example I may want in my application.
It will inherit core properties from UIColor and then I add my own properties to it to define my colour. This would create a custom UIColor object which outputs a blue colour called it SamsFavouriteColour. So when I pass it into this
[UIColor setColorToSamsColour: SamsFavouriteColour.];
The output from this would be my specified colour, my new subclass has modified the UIColor object behaviour to output my own through modifying the behaviour of the setColorToSamsColour method. My new subclass has all the properties of a colour object but I have added additional functionality in it to make what I want. Then by passing into the setColorToSamsColour method it allows modification.
Subclass is important and can be useful especially when your building bigger and more complex applications. Well worth spending time over as it something that you will use time and time again.
Hope this explains it and is helpful to you.
Alex Cevallos
16,551 PointsAlex Cevallos
16,551 PointsYour edit helped me understand.. thank you very much and have a nice day!