29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6. <strong>Smalltalk</strong> Building Blocks<br />

6.1 <strong>Introduction</strong><br />

The aim of this chapter is to present <strong>an</strong> introduction to the <strong>Smalltalk</strong> Programming L<strong>an</strong>guage. The<br />

scope of this chapter is to describe some of the features of the <strong>Smalltalk</strong> l<strong>an</strong>guage. As such it is not<br />

intended to be a comprehensive guide to the <strong>Smalltalk</strong> l<strong>an</strong>guage. For a detailed description of the<br />

<strong>Smalltalk</strong> l<strong>an</strong>guage’s syntax <strong><strong>an</strong>d</strong> most commonly used constructs see, Appendix 1.<br />

The remainder of the chapter is structured in the following m<strong>an</strong>ner: Section two introduces the basic<br />

elements of the <strong>Smalltalk</strong> l<strong>an</strong>guage. Section three discusses the concept of classes <strong><strong>an</strong>d</strong> inst<strong>an</strong>ces in<br />

<strong>Smalltalk</strong> <strong><strong>an</strong>d</strong> how they are defined. Section four presents methods <strong><strong>an</strong>d</strong> method definitions.<br />

6.2 The basics of the l<strong>an</strong>guage<br />

<strong>Smalltalk</strong> is a completely object oriented l<strong>an</strong>guage <strong><strong>an</strong>d</strong> as such has no other concept other th<strong>an</strong> object.<br />

The actual l<strong>an</strong>guage is very, very small, however the <strong>Smalltalk</strong> system (as exemplified by VisualWorks)<br />

is very big. All <strong>Smalltalk</strong> programmers make extensive use of the existing classes e ven when they are<br />

writing relatively trivial code. For example, even when a programmer writes the following version of<br />

the traditional “Hello World ” program they are reusing existing classes (rather th<strong>an</strong> just using the<br />

l<strong>an</strong>guage):<br />

| myName |<br />

myName := 'John Hunt'.<br />

(myName findString: 'Hunt' startingAt: 1)<br />

ifTrue: [Tr<strong>an</strong>script show: 'Hello ' , myName]<br />

ifFalse: [Tr<strong>an</strong>script show: 'Hello World'].<br />

In this example, I have reused the String class to represent the string ‘John Hunt’ <strong><strong>an</strong>d</strong> to find a<br />

substring in it using the message selector findString:startingAt:. Some of you may say that<br />

there is nothing unusual in this <strong><strong>an</strong>d</strong> that in m<strong>an</strong>y l<strong>an</strong>guages string h<strong><strong>an</strong>d</strong>ling facilities are extensions to<br />

the l<strong>an</strong>g uage. However, in this case, the result of the test is <strong>an</strong> inst<strong>an</strong>ce of either True or False<br />

(subclasses of Boole<strong>an</strong> ) <strong><strong>an</strong>d</strong> that this message receives the message ifTrue:ifFalse:. What is<br />

printed to the Tr<strong>an</strong>script obje ct depends on which object actually receives the message. Thus,<br />

ifTrue:ifFalse: is not part of the l<strong>an</strong>guage but a method defined in a class. In most l<strong>an</strong>guages the<br />

if-then-else construct would be <strong>an</strong> inherent part of the l<strong>an</strong>guage - in <strong>Smalltalk</strong> it is not. Th is is because<br />

everything is <strong>an</strong> object in <strong>Smalltalk</strong>, thus all l<strong>an</strong>guage constructs are messages sent to objects. This<br />

feature also illustrates the extent to which existing classes are reused - that is, you c<strong>an</strong>’t help but reuse<br />

existing code in <strong>Smalltalk</strong> because you do so by the very act of programming.<br />

As well as possessing a completely object oriented programming paradigm, <strong>Smalltalk</strong> also possesses<br />

<strong>an</strong> inherit<strong>an</strong>ce mech<strong>an</strong>ism. It is this feature which separates <strong>Smalltalk</strong> from object based l<strong>an</strong>guages such<br />

as Ada - they do not possess inherit<strong>an</strong>ce.<br />

The availability of inherit<strong>an</strong>ce is very import<strong>an</strong>t in <strong>Smalltalk</strong>. For example, it promotes the reuse of<br />

classes as well as enabling the explicit representation of abstract concepts (such as the cl ass Collection)<br />

which c<strong>an</strong> then be turned into concrete concepts (such as the class Set). It is also one of the primary<br />

reasons why <strong>Smalltalk</strong> is so successful as a rapid application development tool - you inherit much of<br />

what you w<strong>an</strong>t <strong><strong>an</strong>d</strong> only define the wa ys in which your application differs from what is already<br />

available.<br />

61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!