If you are planning on being a real-world programmer today (by which I mean someone who gets paid for it), like it or not, you're going to need to understand the concept of object-oriented programming. Languages such as C++, Java, and Visual Basic are used by everyone these days, and nobody is hiring Fortran or C programmers anymore. Object-orientation tends to be a love-it-or-hate-it affair; Some people find it makes things much easier because you can arrange everything into neat, orderly collections of hundreds or thousands of objects and structures. Other people despise having to define all those classes and functions before they can actually get any work done. Whichever one you are, if you've never done object-oriented programming, there's no time like the present to start, unless you're a hobbyist who prefers the simple days when a program could be written on one line, and you don't intend to program as a job anytime soon.

This page attempts to explain the fundamental concepts of OOP (Object-Oriented Programming). It focuses mainly on the different data and structure types. If you can understand these things, you're well on your way to becoming somebody who actually does useful things with object-oriented languages. (Note that this page is not specific to any language, and example commands given are conceptual, not intended to be actual valid programming statements. They're there to help you understand the concepts.)

Variables (Data, attributes, properties)

The most fundamental of all programming abstraction concepts, a variable is a named piece of data. Used in just about every high-level language in the world (including languages like BASIC), a variable holds a particular type of data. For example, suppose you would like the program to store a variable containing your name. Further suppose that this variable was called YourName$. You could set this variable (with a command like YourName$ = "John Smith"), display this variable (with a command like PRINT YourName$), or operate on this variable (with a command like YourName$ = YourName$ + "B.Sc.", which would add "B.Sc." to the end of your name). Variables are the building blocks of all high-level languages, which includes almost any language that isn't assembly language.

In object-oriented programming, variables are often used to store attributes or properties of objects. They are often called the "data" of the object.

Objects

Quite simply, an object is an object. The concept is somewhat nebulous in programming, but then, it is in real life as well. In normal English, an object could be anything: A tree, a box, or a person. Similarly, in programming, an object can be anything that exists in the program. An object has certain data attached to it in the form of variables. In fact, an object is mostly just a collection of variables. For example, if you have an object called "Tom", representing a person named Tom, this object might contain data such as Tom's name, his age, his height, his weight, and so on. These pieces of data, collectively, make up the object.

The popular object-oriented programming languages (most notably C++ and Java) use the industry-standard "dot notation" to refer to objects and their properties. This notation takes the form of objectname.variable. For example, to refer to Tom's age, you would use Tom.age, assuming that the object is called Tom and the variable inside that object is called age. You could refer to this variable just like you would any other variable. For example, you could set Tom's age to be 30 with the command Tom.age = 30.

Classes/Structures

The most all-encompassing concept in object-oriented programming, a structure is essentially a collection of variables, arranged into a standard format. It could be thought of as a template for an object. For example, suppose you are writing a game which is a driving simulation, and there will be 50 types of car in this game. Each car will have various specifications that need to be set, such as top speed, engine horsepower, fuel tank capacity, etc. Now imagine having to create these variables for each car. It is possible, certainly, to do so (making variable names like Corvette.TopSpeed, Lexus.Horsepower, or Porsche.TankSize). However, wouldn't it be easier if there were a standard set of data for each car? You could define a "car" structure to state what variables need to be stored for each car. Then, for each car that you create, you can simply state that it is an object of the class "car", and all the variables for that car object will be created for you. For example, after stating that the "car" structure contains variables called TopSpeed, Horsepower, and TankSize, then you could say to create an object of the class "car" called Corvette. This would automatically create the variables Corvette.TopSpeed, Corvette.Horsepower, and Corvette.TankSize. This simplifies the creation of multiple objects of the same type.

The terms "structure" and "class" are similar, but they carry important implications. The term "structure" dates back to C, which was not considered an object-oriented programming language, even though it did let you make structures, and objects inside those structures. On the other hand, the preferred term today is "class". "Class" was actually made popular by C++, in which a class was very similar to a structure, but with some added functionality. Because C++ was the language that led rather directly to Java, the terminology of "class" became firmly grounded in the world of OOP, and today most Java programmers probably wouldn't even know what you were talking about if you started referring to a "structure" in your program. Use the term "class" instead.

It is crucial that you understand the difference between classes and objects. A class is merely a concept. A class is the concept of a person with a specific height and weight, or a car with a specific speed and colour. An object is an actual instance of such a class. A class is nothing, or rather, it is not a thing; A class does not actually exist as an entity. An object does.

Methods (Functions)

Functions have existed in programming for a long, long time. They make things easier by allowing you to specify a particular procedure which can then be called by a name. For example, suppose you have a program which, as part of its functionality, bakes a cake. Obviously, baking a cake requires multiple steps. Instead of having to specify each command whenever you wanted to bake another cake, you could group all the required commands into a function called BakeCake; Then, whenever you wanted to bake a cake, instead of having to tell the program all the steps involved again, you could just tell it to run the BakeCake function, and it would perform all the steps in that function. Variables are the adjectives of programming, and objects are the nouns; Functions are the verbs.

Functions can also be attached to objects, just like variables can. However, a function which is a part of an object is more properly called a method. And that's really all a method is: A function which exists as a property of an object.

Methods are referred to using dot notation, just like variables. For example, suppose your car class contains a function called StartEngine, to start the engine of a particular car object. This method would then be created for each car object. Thus, if you wanted to start the engine in your Corvette, you'd type Corvette.StartEngine(), and to start the engine in your Porsche, you'd type Porsche.StartEngine(). (The parentheses are used to hold arguments for functions; Most languages use them, and they're required even if you're not passing any arguments to the function.)


If you're still with me, and you actually understood what you just read, congratulations! You've tackled the most nebulous fundamentals of object-oriented programming, and you can now move on to start practicing with an actual language. Good luck!

Back to the main page