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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

58 <strong>Computer</strong> <strong>Programming</strong> <strong>with</strong> <strong>GNU</strong> <strong>Smalltalk</strong><br />

Multiple Inheritance and Single Inheritance<br />

Some programming languages allow programmers to create classes which inherits properties and<br />

behavior from more than one class. So, some classes have more than one superclass. This is called<br />

multiple inheritance. Other inheritance type, which allows only one superclass per class is called single<br />

inheritance.<br />

The examples we have given so far are of single inheritance and the examples we will give from now<br />

on will also be of single inheritance because <strong>Smalltalk</strong> uses single inheritance to manage classes. It is<br />

still very arguable whether multiple inheritance is needed in object oriented design or only makes the<br />

inheritance more complex.<br />

Polymorphism<br />

In a world full of different kinds of objects, some objects may response the same message although<br />

they are different kind of objects, meaning they are instances of different classes. That's why some<br />

programming languages allow us to define methods <strong>with</strong> same selector in different classes. Or they<br />

allow a subclass to change a selector's behavior which it inherited from its superclass. This is called<br />

polymorphism.<br />

Note:<br />

The special case where a subclass changes the behavior of a method it inherited from its superclass is<br />

named as overriding.<br />

We will now give a classical but very explanatory example for this concept. Think of a class called<br />

Animal and its two subclasses Dog and Cat. A method we would implement in Animal class would<br />

be speak because every animal communicates somehow <strong>with</strong> other livings by making some kind of<br />

noise (although dogs prefer to bite sometimes!). But when this message is sent, dogs would bark while<br />

cats would miaow. So they response to the same message but in a different way.<br />

Creating Your Own Classes<br />

<strong>GNU</strong> <strong>Smalltalk</strong> has hundreds of classes built-in but the universe has uncountable of them so <strong>GNU</strong><br />

<strong>Smalltalk</strong> gives us the opportunity to create the classes we want for our needs. We will sometimes call<br />

the classes we made as custom classes.<br />

First, we should talk about what classes are made of. Classes are the blue prints of objects so they<br />

definitely should describe all the things that objects have. For example, they have instance variables to<br />

define the properties of an object. They should have method definitions to make objects responsive to<br />

some certain messages. But they have more. There are class variables. Class variables hold the<br />

information about the state of the class. There are also class methods which only classes can execute<br />

and not the instances of them.

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

Saved successfully!

Ooh no, something went wrong!