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.
#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
Sam Chaudry25,519 Points
Importing 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.