Newbie Question: What is 'private'

Carl Watts Carl at
Thu Feb 18 18:29:37 UTC 1999

I'm glad you asked that question, because I LOVE answering it.

A private method is one that should only be invoked by an object sending the message to itself.

So if 'foo' is the name of a private method, the only acceptable way to invoke it is 'self foo' (the object sends it to itself).

If you are familiar with C, its a cousin to declaring a C function as 'static' (limiting who can call the function to other functions in the same .c file).

It is useful when writing an object to have some messages that are 'public' (any object is allowed to send the message to the object with the public message) and some that are 'private' (only the object itself should send that message to itself).  Private methods contain implementation details of the object that are allowed to change in the future without affecting the public messages (or anyone who uses them).

It is a very useful distinction and is the core idea to one of the most useful aspects of Object-Oriented Programming, namely "Abstraction".

It is true that most Smalltalk systems do not ENFORCE the rule that private methods should only be invoked by the object sending the message to itself. But making the distinction when you write an object still gets you plenty of advantage anyway. Any method you've marked as 'private' you should be able to change in the future without having to consult with anyone else who uses your object. By marking it 'private' you've declared that the method is NOT for other objects to send to you, its only for your object to send to itself.

<BEGIN More Reflective Comment>

Unfortunately the standard code browsers built into Smalltalk were designed for expert programmers who understood this distinction and didn't need their browsers to help them think this way.  For Newbies to Object-Oriented programming it would be beneficial if Smalltalk's browsers made the private/public distinction more obvious.  It is really very important.  I implemented alternate Smalltalk browsers (and others have as well) that support Newbie Smalltalk programmers in understanding this important distinction.  Maybe someday I'll bring them into Squeak and make them public.

<END More Reflective Comment>

<BEGIN Advanced Comment>

I find the private/public distinction so useful that when I program I actually divide 'private' methods into three different flavors of 'private'.  Therefore I use the following 4 categories for messages/methods:
	public: any object can send this message to the receiver
	privileged: only certain other objects (which are explicitely named in a comment) are allowed to send this message to the receiver
	protected: only the implementor and subclasses are allowed to send this message to self
	private: only the implementor is allowed to send this message to self, subclasses should not

I find these 4 levels of public/private useful but most programmers get most of the benefit from just declaring their methods public or private.  By convention, if a method is not marked 'private' its assumed to be 'public'.

<END Advanced Comment>

Carl Watts

More information about the Squeak-dev mailing list