Main Content

Private and Public - and things between

Archive - Originally posted on "The Horse's Mouth" - 2011-01-22 10:25:55 - Graham Ellis

A public train service is one which is available for anyone to travel on; a private one only takes limited passengers as invites / made available by the operator. And there could be intermediate levels too - I understand that at times the British Transport Police protect the last train from Weymouth to Bristol on a Saturday night from the more inebriated members of the public who wish to travel on it. Then you have package trips available - such as [this one] next September, which is open to the public, but only if they join a group.

It gets more interesting when you consider that a public train service can be provided by a private company, and that even within the public accessible railway premises, there are areas which are protected from access from the public and are only available to staff who, however, may not be staff actually employed by the private company that runs the stations and trains.



Why all the rules about who can do what? It would be downright dangerous to allow members of the public, without appropriate health and safety training, do wander around at will even on a publicly owned railway. And if such training was given, and precedents were set, the new system would provide yet another set of rules and regulations that had to be looked after and updated as each new station opened( ;-) ) and each new train type came into service. So the best thing is to allow a single way of doing each operation that's needed by the public user, and then to provide protected or private methods of delivering those requirements.

The first picture illustrate a train at Shiplake, operated by the First group which is private / protected (a plc) but operating a public service from Henly-on-Thames (population 12,000) to Twyford (for London) through village at least once an hour. The second picture illustrates a private railway in a garden in Shiplake. The only trains there are provided and run by the owner for his own use, and it would be an exceptional day indeed if he were to run services for the general public.


Where am I headed? I'm drawing a real life parallel to computer programming, where various elements of a program are often marked "public" or "private", and where there are often levels in between - the most commonly used intermediate word is "protected" and sometimes the word "package" is used too, although each of these words has slightly different meanings depending on the programming language that we're talking about.

Starting with a standard that applies across all languages. If something is described as "public" then it's available to any programmer who wants to use it. And if something is described as "private" then it's available only within the tight area of the programming unit - usually a class - in which it is defined. The public can make use of private elements indirectly, however, by calling up a piece of code marked 'public' within a class which within itself makes a call to a private element.

The beauties of using private members like this are two-fold. Firstly, the private method can be changed later on without the need for the public user to make any changes to allow for it (this is known as encapsulation), and secondly there's only a limited number of facilities that need to be ruggedised and safety audited for use by the public, as private ones have a firm lock on the door!

In PHP, the additional level of "protected" says that a member is available within its own class (so as it would be if it were private), AND within other classes which are derived - either directly or indirectly - from that class. Yesterday, I wrote a new example - a [source code] which included a base class called animal. I extended the animal into a specialized subclass called silentanimal, and then I defined four types more finely - dogs (which bark from time to time, and it depends on the dog how much it barks), sealions (all of which bark 20 times a day), giraffes (which don't bark), and cats (which don't bark either, but which are going to have their names displayed a bit differently). PHP does *not* have an extra package level.

Both C++ and Java also use private, protected and public - and also have a "package" level which is unstated and again comes somewhere in the middle. There's a full set of notes and diagrams that we've provided for Java [here - .pdf file]. And you'll notice that "protected" in Java does not relate to the class inheritance structure, but to Java's packages and subpackages.

In languages such as Perl, it's assumed that the programmer knows what (s)he's doing, and there aren't any of the "private" and "public" words. Which makes for much quicker coding if the assumption is correct, and a nightmare or maintainance if it isn't ...




Public Well House Consultants courses which include Object Orientation:
Perl for Larger Projects
Object Oriented PHP
Python Programming
Learning to program in Python
Ruby Programming
Learning to program in Ruby
Learning to program in Java
Java Bootcamp
Learning to program in C++
C and C++ programming
C++ Programming

Object Oriented techniques, and how they're implemented in each of the languages above, are also covered if required on private (single company) courses which can be run for you at our training centre, or at your offices.


[incr-Tcl] is an Object Oriented extension to Tcl, and we can cover it on private Tcl courses; if you're on either of our public Tcl courses and would like the subject added in, please let the tutor know and he'll be happy to add an extra hour or two before or after lessons on the final day of the course (no extra charge!).

Lua is not strictly an object oriented language, but its metatables provide something that's very close and with the same philosophy; we cover them on our public Lua courses, and they'll usually be covered on private courses too (subject to tailoring - very occasionally they won't be required)