When to use categories and when to use subclassing? [closed]
Asked Answered
S

4

30

Can anybody tell me when to use categories and when to use subclassing in Objective-C? Also please tell me the advantages and disadvantages of them.

Shanta answered 9/11, 2011 at 5:47 Comment(3)
possible duplicate of Difference between inheritance and Categories in Objective-cExtemporaneous
What have you found out about them so far? What are you trying to do with them? What leads you to believe that they are related in any way?Extemporaneous
Category or subclass? Only one point: whether you need a new class.Butyraceous
R
41

An objective-c category is useful if you want to alter the behavior of ALL instances of the class, with minimal code. Subclassing is more useful if you want to alter the behavior of only certain instances, and retain the original method for others.

Categories can be dangerous, especially if you cannot view the source of the original method, so you should generally use subclasses on third-party and private frameworks rather than a category.

Rosales answered 9/11, 2011 at 5:55 Comment(6)
@coneybeare, Can we say that Category does not change the objets where as subclass change the object ? please tellCute
I would say the other way around. A category changes the available methods on an instance of an object, where subclassing creates a new object of a different type.Rosales
@Rosales Can you explain how category can be dangerous for 3rd party or private frameworks?Wei
Because you can be overwriting or altering a private method that can change at any time without you knowing.Rosales
@ coneybeare overwritting can make sense but how could be a private method ? it's private (assuming in scope wise means private to that class). That too if you added any method in category and that method added in future releases by apple. this could also be an issue.Cute
Advantage of category is that we can add method to existing classes for which we don't have source access. I mean we can add methods to standard library or private frameworks.Teena
C
15

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.

Corticosterone answered 9/5, 2014 at 15:5 Comment(0)
M
8

Adding to what coneybeare said. Subclassing is a better option for customization, and Categories are better to be used when you just want to add some functionality to existing classes.

Manzano answered 9/11, 2011 at 10:16 Comment(0)
U
3
  • Do you want to change something which happens as part of framework calls during the lifecycle of a UI object? Use Subclass. Override respective methods, such as init, drawrect, layoutsubviews etc.

  • Do you want something application wide, something which is in
    addition to the existing functionality, and you don't care if this
    becomes available to all instances of this pre-existing instances of the framework class? Use categories. Example: animate UILabel upon certain user action, and apply this animation through out your app to all UILabel instances.

Urine answered 16/12, 2015 at 8:57 Comment(1)
Thanks for the answer. I have a doubt; please correct me if I am wrong - even though category and subclasses can be used interchangeably but what you mentioned above is the best practice. While making this statement I thought about use cases - overriding existing behavior, adding new functions, adding variables to maintain state or any other properties.Pasahow

© 2022 - 2024 — McMap. All rights reserved.