0

These two questions are quite common when we search it but yet I need to get a satisfying answer about both.When ever we search a difference between say subclass and a category we actually get definition of both not the difference.I went to an interview to a very good MNC working on iOS and I was encountered with these two questions and I gave almost all the answers I have read here but the interviewer was not satisfied.He stuck to his questions and was that-

  1. Why do we needed category when we can use a subclass?

  2. Why we needed blocks when we can use functions?

So please explain me what specific qualities blocks and category add in objective C that their counter part can't do.

Imran
  • 1,715
  • 2
  • 20
  • 42
  • please negative points with comments.I can offered some negative points for my concept. – Imran Jun 23 '14 at 11:35
  • 2
    (I did not vote, but) There are some problems with your question: 1) Asking two different unrelated questions in one posting is not a good idea. - 2) I am quite sure that both questions have been answered multiple times already, for #1 see e.g. http://stackoverflow.com/questions/8060884/when-to-use-categories-and-when-to-use-subclassing. If those answers do not satisfy you then you probably should describe *exactly* what part you do not understand, instead of your quite general questions. – Martin R Jun 23 '14 at 11:39

5 Answers5

3

First...

Just reading the documentation "Subclassing Notes" for NSString shows why creating categories is sometimes better than subclassing.

If you wanted to add a function -(void)reverseString (for instance) to NSString then subclassing it is going to be a massive pain in comparison to categories.

Second...

Blocks are useful for capturing scope and context. They can also be passed around. So you can pass a block into an asynchronous call which then may be passed elsewhere. TBH you don't care where the block is passed or where it is finally called from. The scope captured at the time of creating the block is captured too.

Yes, you can use methods too. But they both have different uses.

Your questions are a bit odd. It's like asking...

Why do hammers exist when we can just use wrenches?

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    +1. I especially liked the last comment, that made me chuckle. I've always found `"Security is not something that can be added to software as an afterthought; just as a shed made out of cardboard cannot be made secure by adding a padlock to the door"` from Apples Secure coding guidelines to be quite funny as well – Popeye Jun 23 '14 at 11:43
  • @Popeye Haha :D Thanks, I like that quote. Haven't read that yet. May go have a read now :) – Fogmeister Jun 23 '14 at 11:46
  • 1
    It's in https://developer.apple.com/library/mac/documentation/Security/Conceptual/SecureCodingGuide/Introduction.html#//apple_ref/doc/uid/TP40002477-SW1 – Popeye Jun 23 '14 at 11:49
1
  1. You can't use subclassing when someone else is creating the objects. For instance, NSString is returned from hundreds of system APIs, and you can't change them to return MyImprovedString.

  2. Functions split up the logic; blocks allow you to write it closer together. Like:

    [thing doSomethingAndWhenFinishedDo: ^{ some_other_thing; }];

the same code written with functions would put the second part of the logic several lines away in the file. If you have a few nested scopes in your logic then blocks can really clean it up.

Bryan
  • 11,398
  • 3
  • 53
  • 78
0

Why do we needed category when we can use a subclass?

Categories let you expand the API of existing classes without changing their type. Subclassing does the same thing but introduces a new type. Additionally subclassing lets you add state.

Why we needed blocks when we can use functions?

Block objects are a C-level syntactic and runtime feature. They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory. A block can therefore maintain a set of state (data) that it can use to impact behavior when executed.

You can use blocks to compose function expressions that can be passed to API, optionally stored, and used by multiple threads. Blocks are particularly useful as a callback because the block carries both the code to be executed on callback and the data needed during that execution

  • 1
    Additionally subclassing lets you add state..Can you please elaborate it how it done? – Imran Jun 23 '14 at 12:13
0

Category : It is used if we want to add any method on a given class whose source is not known. This is basically used when we want to alter the behaviour of any Class.

For example : If we want to add a method on NSString to reverse a string we can go for categories.

Subclassing : If we want to modify state as well as behaviour of any class or override any methods to alter the behaviour of the parent class then we go for subclassing.

For example : We subclass UIView to alter its state and behaviour in our iOS code.

Reference :

When to use categories and when to use subclassing?

What is the difference between inheritance and Categories in Objective-C

Community
  • 1
  • 1
Imran
  • 1,715
  • 2
  • 20
  • 42
0
  • We need new method but we don't need new class so we need category.

  • We need function but we don't need named function so we need block.

无夜之星辰
  • 5,426
  • 4
  • 25
  • 48