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
A key difference between Java and Smalltalk is that Smalltalk has first-class class (no pun intended).
A class in Smalltalk is an object. The closest thing to static
method and variable is then class-side method and variable, as mentioned by Frank Shearer.
But this difference is more profound as soon as inheritance is used. In java class-side inheritance does not exists, while it is possible in Smalltalk.
If class A
inherits from B
, and if you have a
and b
which are instances of A
and B
, in Smalltalk, b class
inherits from a class
. This would not be the case in Java where a getClass()
and b getClass()
return instances of Class
, which are not related with each other.
Let's say now that class A
implements the singleton pattern: it has a class-side field instance
and an getter method instance
. Class B
is another object with its own instance
field. As a consequence, A instance
and B instance
will return different object.
This is clearly one of the major difference between Smalltalk and Java from a OO standpoint.
Other difference include the existence of metaclasses, extension methods, duck typing vs static typing, reification of doesNotUnderstand
and few other things that make coding in Smalltalk or Java completely different.
And of course, Smalltalk has closure which Java still lacks.
See also Why doesn’t Java allow overriding of static methods ?