r/iOSProgramming • u/[deleted] • Dec 31 '13
... but what does it mean?
http://codeplease.tumblr.com/post/71741464896/but-what-does-it-mean
0
Upvotes
1
u/blaizedm Objective-C / Swift Dec 31 '13
If someone were to hand you a drawing of a farm, and you wanted to add a cow to that picture, a category would be like drawing the cow directly onto the picture they handed you, whereas a subclass would be like if you traced the picture of the farm onto another piece of paper and then added the cow.
3
u/ObjectiveCopley Dec 31 '13 edited Dec 31 '13
Well, I'm glad I've thought of that exact same question myself. Here is my answer (I already had this in my head):
With a category, imagine your class as a Lego block. A category is another lego block that you snapped on.
Subclassing is like taking a 2x2 lego block, then you take that Lego and add onto it but not by connecting with the pins, but by stretching it out into a more useful 4x2 block (representing the new functionality and the inability to directly separate them back apart)
Not sure how good of an answer, but I think I was asked this a while ago during interviews and this is what I said. Thinking about it, I wonder what are the odds that you interviewed me...!