views:

270

answers:

3

For example:

@interface Fraction: NSObject {
...

When wouldn't NSObject be used and is NSObject the ultimate parent class for all other classes?

Please feel free to correct me on any wrong terminology used.

+4  A: 

If I was writing a subclass of NSView, I would write:

@interface Fraction: NSView {...

because my class is inheriting from NSView. Classes in Objective-C inherit from only a single parent class. As you work your way up the chain from NSView (to its parent class, NSResponder), you would find that it eventually inherits from NSObject.

I'm not sure about the second part of your question. I think all classes in Apple's Cocoa Frameworks eventually inherit from NSObject, but don't quote me on that. There's no reason, in Objective-C, why there couldn't be other root objects (otherwise Objective-C would only be used on Apple systems). However, in Cocoa, NSObject is the root.

Jeff Hellman
oh duh. i see, thanks for the answer!
Devoted
There are a few that don't, like NSProxy and the Objective-C classes (Protocol, Class)
Jason Coco
Class is not a class in Objective-C -- it's a datatype similar to id.
Chuck
+5  A: 

Cocoa has two root classes: NSObject and NSProxy.

NSObject is the root class of almost all Cocoa objects.

NSProxy is an abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet, and is the root class for classes like NSDistantObject.

Both classes implement the NSObject protocol.

It is also possible to write your own Objective C classes that do not have a root class, but you would probably never do that, and you would not be able to use it with Cocoa for much of anything unless you also implemented the NSObject protocol, and even then it would be of dubious use.

Peter N Lewis
Correct. Unlike Java, where Object is the default parent object, in Objective-C, if you don't declare a parent class you don't get one. Effectively, your class becomes a root class, and you get zero inherited behavior. This is almost never what you want, and will cause all sorts of strange compiler warnings.
Quinn Taylor
@Quinn, it is possible to do though, but it involves calling the Objective-C runtime functions directly in order to properly allocate instances and register bits and pieces. I've done it once as an experiment and got nowhere near the functionality of NSObject before giving up.
dreamlax
Root class, not root object. Objective-C isn't a prototype-based OO environment.
NSResponder
+2  A: 

In any Cocoa app, if you examine the runtime class tree, you will find five root classes: NSObject, Object, NSProxy, NSMessageBuilder, and NSZombie.

Most ordinary objects in your app will be descended from NSObject. Object is the original root class from the early days of Objective-C, and it was superseded by NSObject when NeXT was developing the Enterprise Objects Framework.

NSMessageBuilder is used by the runtime for message forwarding. NSProxy is used by the distributed objects implementation to deal with marshaling messages to send to objects in other processes or even on other hosts. NSZombie is a class that's used for debugging memory leaks.

NSResponder