3 Class as a package template

A class in object oriented programming (OOP) is a template that has specific slots. Each slot is identified by its name and may have a type associated. For example, a “Question” class that represents an exam question may have the following slots (technically called “fields” or “properties”):

Note that “Question” (with an upper case “Q”) is a template. In other words, the Question class cannot be used to store information of a particular question. Instead, it is a “cookie cutter” used to cut out actual question cookies.

Remember this: a class is a cookie cutter, an object (all variables are objects) is a cookie.

A program may create an object of the class “Question” to represent a particular question. Because all the properties of a question are already packaged in the object (let’s called it “aQuestion”), a programmer can write a subroutine/procedure that prints out a question in an HTML document.

The nice part, now that we have the concept of a class, is that the procedure can specify just one parameter: an object of the class “Question”! As a result, there is no tedium to specify all five parameters individually. There is no chance of confusing one parameter with another.

In summary, a class in OOP is very useful as a packaging mechanism. Packaging is good because we can pass an entire package as a single parameter, hence reducing the number of parameters. This, in return, helps to minimize mistakes made in passing parmaeters.

Of course, OOP is not only about packaging. Packaging is a useful concept, but it still leaves much to be desired.