views:

72

answers:

2

Hi all,

This may be a silly question. I need to know where is the +alloc method is defined for any Objective-C class? If it is a class method in NSObject how it is inherited? Because class methods cant be inherited. Thanks.

+5  A: 

Yes, it is defined in NSObject (and NSProxy). A quick look in the documentation would have told you that.

because class methods cant be inherited.

Wrong. Where did you read that?

Ole Begemann
A: 

It's a class method of NSObject. NSObject itself does not inherit from anything because it's a root class.

alloc


Returns a new instance of the receiving class.

+ (id)alloc

Return Value
A new instance of the receiver.

BoltClock
-1; objective-c doesn't have static methods. You must be thinking of java.
Graham Lee
@Graham: Thanks for spotting that, I've corrected it.
BoltClock
@Graham Lee, Even though we call it class methods in objective c, its quite similar to Java static methods. We just call it by the class name directly without instantiating objects.
charith
@charith: in Objective-C, `static` has a specific meaning. That meaning is not satisfied by a class method.
Graham Lee
@Graham, Shall I know what it is? Im new to Objective C. Also I cant imagine how class methods are inherited. Pls explain. Thanks.
charith
@charith: You don't call methods in Objective-C, you send messages. To send a message you must have an object to send it to. The only difference between a class method and a normal method is that you send the message to an object that happens to represent a class.
JeremyP