Objective-C

Started by
8 comments, last by smc 15 years, 3 months ago
I've been looking at Objective-C and I am failing to understand the fundamental difference between C++ OO and Objective-C/Smalltalk OO. I understand that in Obj-C you pass messages to objects instead of calling methods directly, but what is the difference besides semantics? I know that there is something I'm missing.
Advertisement
Wikipedia's entry on Objective-C has a fairly good discussion of the differences of the object models. But, in a nutshell, it's basically the difference between static typing and dynamic typing. In C++, you can't call a function on an object unless you're sure at compile time that object supports that function. In Objective-C, you can send a message to an object even if at compile time that can't be determined that the object will process that message.
Yeah, I read the Wikipedia entry, which is where I read up on the differences. I just couldn't really understand how these differences impact the programmer. Thanks for helping clarify it, though.

So would you say that Obj-C OO is similar to parametric polymophism a la SML? Where you can pass in any object at will perform the method as long as the type has that method?
Quote:Original post by yaroslavd
Yeah, I read the Wikipedia entry, which is where I read up on the differences. I just couldn't really understand how these differences impact the programmer. Thanks for helping clarify it, though.

So would you say that Obj-C OO is similar to parametric polymophism a la SML? Where you can pass in any object at will perform the method as long as the type has that method?


Unless I am missing something, that is what dynamic typing is, and Objective-C is dynamic.
It's been literally over a decade since I've used Standard ML, but from what I recall, it is different. SML has static typing with parametric polymorphism. You still need to determine at compile time that an object supports a given operation, it's just that type inference means that the same function can be applied to different static types and perform different things for different types based on their types. However, when talking about Objective C, you're dealing with actual runtime dynamic typing: an message can be sent to an object whose interface lacks a handler for that message at compile time.
Quote:Original post by SiCrane
However, when talking about Objective C, you're dealing with actual runtime dynamic typing: an message can be sent to an object whose interface lacks a handler for that message at compile time.

What happens then?
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
What happens is called forwarding. In GNUStep the function forwardInvocation is called and passed an object that contains the function name(called a selector) and parameters. Otherwise doesNotRecognizeSelector is called which raises an exception.
I just finished the Wikipedia article for the second time. I am still a bit fuzzy on how polymorphism works in Obj-C. Do superclasses forward messages to subclasses? Other way around? Of course in C++ if you call SuperClass.someMethod() it will delegate appropriately to SubClass.someMethod(). Does the same thing happen in Obj-C?
Quote:Original post by yaroslavd
I just finished the Wikipedia article for the second time. I am still a bit fuzzy on how polymorphism works in Obj-C. Do superclasses forward messages to subclasses?

No, the classes are not involved. This is about the objects - the instances of the classes. A class specifies that an object of its type responds to a set of selectors (message names and parameters). At compile time, however, we don't know the type of the object. Oh, sure, we know the type of the pointer we're holding, which allows us to make assumptions about what selectors are available and will be responded to, but only at runtime can the language resolve the true type of the object instance.

Think of it this way: imagine if all classes in C++ were part of a single hierarchy, but dynamic_cast neither threw an exception nor returned null when you cast between "unrelated" (more correctly, indirectly related) types. If A and B both inherit from Object and you tried to call methods of B on an instance of A via a pointer-to-member function, only at runtime would you determine that you had the wrong mix of type and method.

Quote:Of course in C++ if you call SuperClass.someMethod() it will delegate appropriately to SubClass.someMethod(). Does the same thing happen in Obj-C?

If a parent type and a child type both implement a selector and you hold a child type instance via a parent type pointer, yes, the child implementation will respond.

This is a good tutorial for C++ programmers.
∫Mc

This topic is closed to new replies.

Advertisement