Hand Crafted Software

 

A Brief Introduction of Object Orientated Programming

It would be a very poor programmer indeed who would attempt to tackle a large programming project without breaking that project down into manageable portions. Terms like module, procedure, sub-routine and function have been part of the programming lexicon ever since the concept of programming a computer was first invented, back in the middle of the twentieth century. This structured approach to developing software has proved to be perfectly adequate in a great many situations, especially when dealing with small to medium sized projects, but as computer systems became ever more complicated, it soon became apparent that a new programming paradigm was required. That paradigm is Object Orientated Programming.

Object Orientated Programming (OOP) is essentially a way of taking the philosophy of structured programming to a new level of sophistication. Now it is not just the code that is broken down to its basic elements, but the data, too. In an object orientated program, the basic building block of a program is the object - a single, discrete piece of data that is packaged together with the code that is specifically needed to manipulate that data. An object is self-contained, therefore; a 'black box' that can be incorporated into a program without the programmer needing to know any of the specifics of what actually goes on inside.

The golden rule in OOP is that no object is allowed to interfere with the internal workings of another object. Instead, objects have to communicate with each other using messages. When an object receives a message from another object, it then acts upon it accordingly, and such actions it takes may involve sending messages to other objects. A message may be a request to an object to provide information about itself, in which case the receiving object responds by sending a message back that contains the information requested, or - if that information isn't available - an explanation of why the request cannot be fulfilled.

In most OOP languages, the act of 'sending a message' is in fact performed in a similar manner to making a function call in C. However, there are no global functions in OOP - the 'function' (or, to use Java terminology, the 'method') that is invoked in such a call is always an integral part of the object and always referenced via the object that it belongs to. At all times, the object is the prime focus of attention - this is why the paradigm is described as 'Object-Orientated'.

Every object is an instance of a particular class. A class is essentially a description of a possible object - a definition of the data it represents (this is essentially the same as a struct or typedef in C) and all the code that is needed to manipulate that data. Depending upon circumstances, a particular class may be represented within an application by just a single object, or by many. In most OOP languages, more complex data structures such as arrays of objects are also permitted. Each object within an array will be a separate object but generally all those objects will belong to the same class.

Classes are organized into a hierarchy. Classes that share specific features are grouped together into super-classes, and because the methods belonging to a super-class are also available to all the child-classes within that branch of the hierarchy, methods that need to me made available to more than one class can therefore be shared without redundancy.

Any existing class can be 'extended' to create a new sub-class. A sub-class will inherit all the methods and attributes of the parent class, but new methods can be written that add new functionality, or override existing methods from the parent class. Because extending an existing class has no effect on the operation of the parent class, new functionality can be painlessly introduced into a developing project in the form of new sub-classes without running the risk of breaking any of the existing code.

Most Object Orientated Programming languages allow a technique knowns as 'method overloading'. This means that two or more methods can be defined with the same name but with differences in the argument list, such as having a different number of arguments, or different types of arguments. The compiler distinguishes between overloaded methods and makes sure that the correct one is called at runtime. Overloaded methods generally perform the same basic operation as one another, but they allow the programmer to specify arguments in different ways depending upon what is convenient in a particular situation.

There are many Object Orientated Programming languages available, and many conventional languages have been extended to include object orientated features. Currently, the two most popular Object Orientated Programming languages are C++ and Java.

As the title of this article suggests, this is intended as being only a very brief overview of the principles of Object Orientated Programming. For more information I suggest that you consult web sites relating to specific OOP languages.

 
Back Home

© 2003 Richard Young. All rights reserved.