Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the key differences between OO in Smalltalk and Java?

What are the key differences between OO in Smalltalk and Java?

Please note that I am a Java programmer trying to expand his horizons by exploring Smalltalk. Currently I know almost nothing about Smalltalk except that it's purer than Java. Therefore I'll prefer the answer that shows how various Java concepts map to corresponding Smalltalk concepts and then introduces the Smalltalk concepts that don't exist in Java at all.

like image 295
Jim Avatar asked Jun 23 '10 14:06

Jim


People also ask

What is difference between oops and Java?

Class and Object are the two most important concepts of Object-oriented programming language (OOPS) e.g. Java. The main difference between a Class and an Object in Java is that class is a blueprint to create different objects of the same type.

What does oo mean in Java?

OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Is Java an OO language?

Java is an object-oriented programming language where every program has at least one class. Programs are often built from many classes and objects, which are the instances of a class.

What is Smalltalk in Java?

Smalltalk do not have a syntax. Instead it has a simple, consistent format for sending messages. Java, like other languages of the C family, has a complex syntax. Environment. Most Smalltalk implementations provide a complete, standalone, live computing environment with image based persistence.


1 Answers

Message passing

Smalltalk uses message passing, not method invocation. The distinction is subtle, but enormously powerful.

Some terminology: Given foo bar: baz, #bar: is a selector, foo is the receiver of a message called #bar: (the # indicates a symbol, much like Common Lisp would say 'bar (or even more appropriately, :bar)), and baz is an argument or parameter. When the line's executed, foo is sent the message #:bar: with argument baz. So far, it's pretty normal. In Java it would look like foo.bar(baz);.

In Java, the runtime system would figure out foo's actual type, find the most appropriate method, and run it.

Things look almost the same in Smalltalk. When you send an object a message, it searches in its method dictionary for a method whose name matches that of the selector of the message. If it can't find one, it searches in its superclass' method dictionary, and so on. Pretty normal stuff.

If it can't find any matching method, it sends itself the #doesNotUnderstand: message, with the original message as a parameter. (Yes, a message send is an object.) But #doesNotUnderstand: is also just a method. You can override it.

For instance, you can have an object that responds to some set of messages while forwarding any other messages it receives to some delegate object. Override #doesNotUnderstand: and hey presto, you have a proxy that will need no maintenance to keep its protocol in sync with the delegate.

Trivial syntax

No, I'm not joking. Smalltalk's entire grammar's maybe 15 lines long. The JLS is... not. Why care? A simple syntax makes it simple to tear a chunk of code apart. Metaprogramming! Refactoring!

No syntax for:

  • conditional statements: (n < 3) ifTrue: ['yes'] ifFalse: ['no']
  • for loops: 1 to: 10 do: [:i | Transcript show: i asString]
  • try-catch: [i := i / 0] ifError: ['oops!']
  • try-finally: [i := i / 0] ensure: [stream close]

And notice all those []s - first-class closures with a clean syntax.

like image 173
Frank Shearar Avatar answered Oct 15 '22 02:10

Frank Shearar