0

This is part of the code for a class method in the class BNRItem, which returns an id object. The author wrote that the key word self should be used when allocating, so that subclasses can also access this method. But wouldn't subclasses be unable to use this method because it returns an object of type BNRItem? Could you tell me what I'm missing here? ;p

BNRItem *newItem = [[self alloc] initWithItemName:randomName
                                   valueInDollars:randomValue
                                     serialNumber:randomSerialNumber];
return newItem;
stumped
  • 3,235
  • 7
  • 43
  • 76

1 Answers1

0

Self refers to the actual class from which you call the method. If a subclass overrides this method, self no more refers to BNRItem, but to the subclass itself.

  • But the object is being assigned to a `BNRItem` type object. So how will the subclass access `newItem`? – stumped Aug 10 '12 at 20:49
  • I don't see the point in that question. The actual type of an object doesn't depend on how it's declared - Objective-C is a dynamic language. –  Aug 10 '12 at 20:58