In Smalltalk we say "send a message to an object", but is it also correct to say "send a message to an object" in Java, or is it only correct in Java to say "call a method of an object"?
It is therefore "correct" (i.e. defendable with objective arguments in a discussion) in any OO programming language, Java included. However, in a pure Java context, it might sound weird: "method calling" is more accurate and less ambiguous, since "message passing" is often used to refer to IPC and network communications.
While this is just terminology, "sending a message" is a very strict concept in Smalltalk which defines how you program, and it can't be followed to a full extent in Java.
In Smalltalk when you see code like:
you know that the object behind the variable
anObject will receive the
doSomething message. Then you have no idea what is going to happen (of course you can assume what kind of object you have and how it will respond, but conceptually that's it). On the other side, when the object receives your message it will:
- Use its own method that matches the message's signature to reply
- Lookup the method in the inheritance hierarchy
- Execute the doesNotUnderstand: method (which by default rises the "message not understood" exception but can be overridden like everything else.
In Java it's hard to explain everything as "message sends" because there are my other ways how things work. Imagine this example:
String text = "value"; System.out.println(text.length());
You can say that you send the message
length() to the object behind the
text variable. But what happens in this case:
String text = null; System.out.println(text.length());
it still looks the same, but no object is receiving the message, because null is not an object in Java (while it is an object in Smalltalk).
One can come up with many examples like this one, but while Smalltalk was built around messages, Java just borrowed some ideas from Smalltalk. Thus it's clear that some concepts of Smalltalk are not fully applicable to Java
Both SmallTalk and Java are object-oriented languages. Message-passing in SmallTalk is just another way of saying "calling a method," but the focus is on abstractions.
Passing messages between objects was meant as an illustration to differentiate OO languages from procedural languages of the day. The focus is more on the way objects interact rather than the specific algorithms that manipulate data. Passing messages implies the sender of a message does not know the details of how that message is processed, and doesn't need to. The message sender deals with an abstraction, rather than something concrete.
Since Java is another object-oriented language that supports similar levels of abstraction, the same concept applies.
I would argue that the term “message sending” is on the one hand useful as an abstract concept to describe a way to think about object oriented programming. In this regard it might be uncommon but you could talk about any object oriented program this way, including one written in Java.
But then there are languages that really implement “message sending”. This is done for example in Smalltalk and in Objective-C. It would not be a correct description of the way Java decides which method to execute.
There is a question on this site that discusses a similar problem: So what did Alan Kay really mean by the term “object-oriented”?
And Alan Kay did comment on one of the answers himself:
I was too blythe about the term back in the 60s and should have chosen something like “message oriented”
You could also have a look at the answers to What's so special about message passing in Smalltalk? on StackOverflow.