Hi,

The "class vs instance" side is one of the most confusing things for
newcomers, I remember struggling with it when I learnt Smalltalk (and
also was a novice in OOP).

It helps you thinking it this way: the instance side is everything
that will affect all instances of such class, and you can think the
"class side" as what affect the factory for these instances (the sole
instance of the class).

E.g. let's say you have a "Dog".
Dog instances will have a #bark method, and maybe a "color" property
(via an instance variable).

So when you do:
dog1 := Dog new.
dog1 color: Color white.
dog2 := Dog new.
dog2 color: Color black.

You have two instances of Dog, and each one with its own color. Both
understand the #bark message.

But the Dog (capitalized) there, references the Dog class itself (aka
"the class side" of Dog) and #new is a method of the "class side" of
Dog.

So if you implement a #newWhite method in the "class side" of Dog, it
would be something like this.
Dog class>>newWhite
  "Returns a new white instance of receiver."
  ^self new color: Color white

In this method the "self new" refers to the class itself, not to the
"instance" of the dog, but the returned object of "new" is an
instance, and there the #color: message is sent to the instance of
Dog.

I hope this explanation helps, the meta relations involved in this are
more complex than what I explained, but I hope this helps you get
started.

Regards!

Esteban A. Maringolo

On Tue, Jul 28, 2020 at 8:35 PM G B via Pharo-users
<pharo-users@lists.pharo.org> wrote:
>
> Being new not only to Smalltalk, but OOP in general, I think I finally am 
> understanding things. One area I am still unsure about is the class side 
> versus the instance side. Does one usually use the class side when they want 
> those inherited in every subclass, which frees one from having to declare 
> them in every instance?
>
> TIA for reading my silly questions.

Reply via email to