Computer Programming with GNU Smalltalk - Free
Computer Programming with GNU Smalltalk - Free
Computer Programming with GNU Smalltalk - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Objects, Messages and Classes: Part II 55<br />
Chapter 5: Objects, Messages and Classes: Part II<br />
Encapsulation<br />
Inheritance<br />
Ever wondered how the electronic circuits inside your cell phone works? Ever needed to know that?<br />
Most probably not. Because, we don't need to know all the manufacturing details about our cell phone<br />
to use it. Just reading the manual is enough to use all of its features. So, we cannot know all details, all<br />
scientific laws, all properties of an object we use and we don't need to. Object oriented programming<br />
introduces encapsulation concept to apply this real life fact in our applications.<br />
<strong>Smalltalk</strong> does not allow you to directly modify internal state of an object. Objects can only be<br />
communicated by sending messages to them. So, <strong>Smalltalk</strong> encapsulates the unnecessary information.<br />
Beside encapsulation allows us to think in a real world manner, it also provides some kind of<br />
information hiding which helps our program to be solid and reusable. This means, if you hide an<br />
object's implementation from the programmer and provide him/her just a protocol to communicate <strong>with</strong><br />
that object then the programmer won't be affected by implementation changes made in the future as<br />
long as the protocol is preserved.<br />
For example, you created a Calculator class which is able to calculate the roots of a second degree<br />
equation. You just send the equation's information to it and it returns the roots... Maybe it uses an<br />
inefficient algorithm which causes it to calculate the roots in hours. But, well, it is better than nothing<br />
and you distributed the code to friends (we shall call them clients). Maybe, after distribution, you found<br />
a revolutionary approach which reduces the calculation time into seconds. The algorithm is changed but<br />
the protocol does not need to because the clients just need to provide the equality to the calculator and<br />
want it to solve the equation. How calculator solves the equation is not their business. So you can<br />
benefit the advantages of encapsulation and distribute your new code to clients. Clients just need to<br />
replace the Calculator class and does not need to change their own program.<br />
Human beings like categorizing things. Because categorizing brings order and order brings easymanipulation.<br />
Similar elements come together to form groups (or categories) and some special<br />
properties of them are the thing which distinguishes them from other group members. We may group<br />
some groups together, according to their similarities to form some other groups and also group them to<br />
form other groups. This process can go on until we reach one main group which is an ancestor of all<br />
others. This is actually a simulation of life itself.<br />
The elements we mentioned above are objects and the groups are classes. What is the most general<br />
expression we use for elements? The answer is object. We can call everything as an object and then<br />
derive a special kind of object from it to which we give names like car, house, animal etc. But<br />
remember, we call objects which we use to create instances as classes. So, when we say car, house,