Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In Objective C you have the concept of sending messages to other objects, and, well this is very similar to method calling in languages like C# and Java.

But what exactly are the subtle differences? How should I think of messaging when thinking about my code?

Note: Just a bit of background here, I'm a C#/Java developer trying to understand some concepts about Objective C.

share|improve this question
2  
Since they're all different languages, the differences aren't subtle. They're different languages. "when thinking about my code"? What code? When thinking about Java or C#, you don't think about messages. You think about methods. Can you clarify how unrelated languages with unrelated concepts can have "subtle" differences? –  S.Lott Feb 11 '11 at 11:49
1  
Please ask you question at stackoverflow.com –  Amir Rezaei Feb 11 '11 at 12:13
    
@S.Lott The question's about the difference between invoking a method and sending a message. The tags are simply misleading. –  Frank Shearar Feb 11 '11 at 14:58
1  
@Vidar, the question is not subjective. You're looking for a textbook definition. Programmers is more for opinions, experience, and subjective questions. –  Stephen Furlani Feb 11 '11 at 15:31
1  
OK - is there a way to get the moderator to move this question to StackOverflow? –  Vidar Feb 11 '11 at 15:50

5 Answers 5

A message is the name of a selector, and the parameters for that selector.

A selector is a symbol.

A method is a piece of code in a class identified by a selector.

In other words, [foo bar: baz] says "send the message called @selector(bar:) with parameter baz to object foo. You could send that message to many different objects.

In contrast, the method bar: for a Foo might look like

-(int)bar:(int)n {
  return n + 1;
}

but for a FooTwo might look like

-(int)bar:(int)n {
  return n + 2;
}

(I hope I have the syntax right; it's been a while since I last touched Objective-C.)

When you send the message, the Objective-C kernel dispatches the message to foo which decides whether it understands the message. It decides this based on whether it can find a method identified by that selector.

Two methods with the same name, and one message.

It's also possible for an object to simply forward a particular message (or set of messages) to another object for processing. In this case, you send a message to this proxy object, which has no methods to match that message, and the proxy forwards the message to its wrapped object.

share|improve this answer

From a purely theoretical viewpoint, there's no difference between the two at all -- there have been a number of formal proofs showing that the two are completely equivalent, and either can be implemented entirely in terms of the other.

From a slightly less theoretical viewpoint, there is one possible difference: in a typical implementation, the virtual function table is statically allocated and the content of each vtable is fixed at compile time. Message lookup, by contrast, is typically done with some sort of map-like object, which is typically dynamic, meaning you can modify it at runtime. This makes it relatively easy to add a new response to a message in an existing class. Unfortunately, in most cases this remains mostly theoretical. First, you're basically dealing with self-modifying code, which most people decided was a pretty bad idea a long time ago. Second, to make it very meaningful, you pretty much need to be able to compile new code into the existing class to respond to the new message you support. Without that, about all you gain is the ability to dynamically add a new name for an existing method.

As implied by the end of the previous paragraph, from a truly practical viewpoint, there's very little difference between the two at all. They're simply two (very slightly) different ways of supporting late binding. Although message-based lookup is generally a bit slower, it would be pretty unusual for the difference to be truly significant. For most practical purposes, they're just two different ways of accomplishing the same thing.

share|improve this answer
2  
Do you have references showing the proofs? I'd like to take a look. There's a vast difference between Java's method invocation and Smalltalk's message sending, not just because of the late binding, but also because of the decoupling of sender and receiver: you can't tell whether the receiver of a message processes it, or forwards the message on, for instance. –  Frank Shearar Feb 12 '11 at 0:30

Usually method calls are resolved at compile time (unless you use reflection in Java), while messages in Objective C are dispatched at run time.

share|improve this answer
2  
If method calls are resolved are compile time, you aren't using OOP, you're using syntactic sugar for overloaded functions taking a struct as first parameter. Late bindung is an essential part of polymorphism and therefore of OOP. –  delnan Feb 11 '11 at 11:42
2  
Yes. But still you can only code a method call against something (in Java) that is well known at compile time. Calling MyObject.foo() will give an error if MyObject or MyInterface has no method foo() defined. ObjC will allow to send you a message 'foo' to object MyObject - and if MyObject has no 'foo', this will bomb at run time. –  Heiko Rupp Feb 11 '11 at 11:47
    
I thought Objective C was a compiled language? –  Vidar Feb 11 '11 at 11:47
    
That's unrealted to early/late binding (dynamic typing vs. structural typig - in your example, the method calls are checked at compiletime but not necessarily dispatched). @Vidar: It is, but it adds magic for dynamic features. You can also compile Python. –  delnan Feb 11 '11 at 11:53
    
@Vidar: It's not an issue of compiled vs. interpreted, but rather static vs. dynamic. In static OOP languages like Java, the compiler checks to make sure that a class defines a method during the compilation phase. In a dynamically-typed language like Objective-C, messages are passed at runtime. –  mipadi Feb 11 '11 at 14:13

In Objective-C, messages are late-bound. That is they are resolved at runtime. C# supports a similar construct through the Dynamic keyword which declares an object as being late-bound as well.

share|improve this answer

Messages are either handled by the kernel or by the language itself (for ObjC for example, there is a very small assembly code that does it).

In the linux kernel for example, messages are done with system calls/function: you can find about them if you search about unix system programming.

The core difference between a method call and a message is this:

  • a method call only happens in your code: in ASM it's translated by a PUSH of the passed arguments.

  • a kernel message is mostly something sent to the kernel which is tracked and send back to a certain processes. I may mistake them for pipes, but whatever: know there already is a mechanisms that allows you to run several programs at the same time and let communicate at the same time. Of course, don't hope that this will work the same way on windows or other OS.

share|improve this answer
    
This isn't "message passing" as it applies to programming languages. –  mipadi Feb 11 '11 at 14:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.