06.06.2013 Views

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - 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.

Objects, Messages and Classes: Part II 77<br />

has and it already has the capability of listing those features. We can either copy and paste the same<br />

code into the new listYourFeatures method or we can call the method defined inside the<br />

superclass. The latter way is preferred by programmers because it reduces the repetition of the code<br />

which provides less work when refactoring your application. For example, our code won't be affected<br />

whenever a new property is introduced to the Watch class because the changes are automatically<br />

reflected to the output by using the super keyword, otherwise we would have to copy the changed<br />

code into our WaterResistantWatch every time.<br />

It would be erroneous if we used the self keyword instead, because it would call itself recursively and<br />

this would cause an infinite cycle. Also it wouldn't give us the effect we wanted because the code we<br />

need is written in superclass' definition not in the class we are doing implementation right now.<br />

In the rest of the program we define two watches which are of Watch and WaterResistantWatch<br />

class and list their features after setting their properties. As we expected, the second watch displayed the<br />

properties it inherited from its superclass, as well as its special property resistanceDepth.<br />

Review Questions<br />

1. Explain polymorphism, inheritance, and encapsulation in a few sentences.<br />

2. Now, think of symbol > as better than and not greater than. Create a Man class which inherits from<br />

the Human class we created before <strong>with</strong> additional instance variables money and handsomeness.<br />

You know for us, men, appearance is not important, the important thing is the beauty inside. So<br />

create the Woman class <strong>with</strong> additional instance variables honesty and generosity. All this<br />

instance variables will get an integer point over ten.<br />

Implement the > method in Man class such that it sums the points of money and handsomeness<br />

and returns true if the receiver's point is higher. Add an exception to this rule, which is, if the<br />

name of one of the Man objects is "Canol Gökel" then it is always the better Man.<br />

Implement the > method in Women class in a similar manner. Sum the points of honesty and<br />

generosity, than return a true or false object, accordingly. There is no need for an exception<br />

this time, after all, all the womens are the same...<br />

3. Extend the Number class so that it can respond the cubed message by returning the cube of the<br />

receiver.<br />

4. Explain the purpose of self and super keywords and their difference from each other.

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

Saved successfully!

Ooh no, something went wrong!