Understanding abstract superclass NSNumber

objective-c

Solution

It's done through class cluster pattern. From documentation:

The abstract superclass in a class cluster must declare methods for creating instances of its private subclasses. It’s the superclass’s responsibility to dispense an object of the proper subclass based on the creation method that you invoke—you don’t, and can’t, choose the class of the instance.

Whenever you create number with some factory method, like `+numberWithInt:` the factory returns instance of concrete subclass. Afterwards, when you call something like `-stringValue:` this selector is sent to instance of concrete `NSNumber` subclass - int in this case. So, `NSNumber` factory methods actually does not return `NSNumber` objects - they return concrete subclasses. Same is true for other Cocoa class clusters - `NSArray`, `NSDictionary`, `NSSet`.

Problem

I am reading some book about `objective C` ,and they say there that `NSNumber` is an `abstract` `superclass` of many `subclasses` that we can use . So, "when we call a method in `NSNumber`, the appropriate subclass is used" . This is not going with some other rule that i know : if `superclass A`, has `subclass B` , and you calling a method in the super class A , that is in the subclass B, you can't do that- because inheritance is working all the way up and not down. So, how is that the `superclass (abstract) class- NSNumber`, is using its `subclass` methods ?? What is the hierarchy here ? Thanks .

Original source