Question about pharo's object model

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about pharo's object model

Pharo Smalltalk Users mailing list
hello guys
I'm new to programming, I started programming using pharo, I had a question
about pharo's object model. In object model we say, methods are public. What
does "public" mean here?
thanks



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Question about pharo's object model

Ben Coman
Generically across programming languages...
- a private method can only me invoked by an object of the same class (or subclass) i.e. it can only be invoked by itself.
- a public method can be invoked from any object.

In Pharo, variables are private and methods are public.

The advantage of private methods is to avoid users calling utility methods that only do half the work to leave data in a consistent state.
Also its clear which methods are outside you public API so you are free you change those without impacting users.

Rather than such a forced constrint, Pharo deals with private methods by convention: 
- prepending "private" or "basic" to the front of the method name
- placing methods into a protocol (or method category) named "private".  
- having a protocol "api" for the public api and everything else to be considered private
Note that "protocols" have no impact on program execution - they are just an organisational tool.

All reflects Pharo philosophy... 
Reduced forced constraints making it quicker and easier to program "in the flow"
i.e. you "can" break convention and call "private" methods if that is pragmatic and you are willing 
to accept the risk that you don't properly comprehend the behavior or the behaviour changes later without warning.

cheers -ben







On Thu, 15 Nov 2018 at 12:44, iu136 via Pharo-users <[hidden email]> wrote:
hello guys
I'm new to programming, I started programming using pharo, I had a question
about pharo's object model. In object model we say, methods are public. What
does "public" mean here?
thanks



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Question about pharo's object model

Julien Delplanque-2
In reply to this post by Pharo Smalltalk Users mailing list
Hello and welcome :-)

When a method is «  public » , it means that it is usable by any other object in the system.

In Pharo, methods are «  public »  and instance variables are « protected ». 

«  protected » means that the instance variable is only usable from inside the class defining it or from subclasses of the class defining it.

Some languages (as Java) define the concepts of « private » method / instance variable which means that the method / instance variable can only be accessed from the class defining it (and not its subclasses) and «  package » which means that the method / instance variable can only be accessed from classes inside the same package.

In fact, in java you can choose the visibility you want for each method / instance variable. In Pharo, this is fixed, methods are «  public »  and instance variables are « protected ». So you do not need to bother.

All these concepts are related to encapsulation [1] which is an important concept of OOP.

BTW, there are some posts related to your question on StackOverflow [2] which I recommend to you.

Cheers,

Julien

Links:

---
Julien Delplanque
Doctorant à l’Université de Lille
Bâtiment B 40, Avenue Halley 59650 Villeneuve d'Ascq
Numéro de téléphone: +333 59 35 86 40

Le 15 nov. 2018 à 05:43, iu136 via Pharo-users <[hidden email]> a écrit :


De: iu136 <[hidden email]>
Objet: Question about pharo's object model
Date: 15 novembre 2018 à 05:43:45 UTC+1


hello guys
I'm new to programming, I started programming using pharo, I had a question
about pharo's object model. In object model we say, methods are public. What
does "public" mean here?
thanks



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html