Question

I know that one should not overide a method in a category.

I wonder if the following is possible. Experts in Objective-C are welcomed to answer!

  1. I have two parent/child classes : Class : ParentClass

  2. For each class, I have a category : ParentClass(Category) and Class(Category)

  3. In each of these category, I have a method -(void)additionalMethod. The methods have the same name and are defined in the category (they don't exist in the classes)

Was it helpful?

Solution

From "Customizing Existing Classes" in the "Programming with Objective-C" guide:

At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.

So the answer to your question is YES. There is no difference between overriding a method in the subclass itself or in a category of the subclass. It makes also no difference if the overridden method is implemented in the base class itself or in a category of the base class.

The only thing you cannot do is to use a category to override a method of the same class or of another category of the class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top