Main Content

Introduction to Object Oriented Programming

Archive - Originally posted on "The Horse's Mouth" - 2008-12-06 09:09:21 - Graham Ellis

"Object Oriented Programming" is a whole new philosophy for programmers who have been writing short scripts for years, and it can be quite frightening to learn with all these new buzzwords like "overriding", "encapsulation" and "polymorphism" creeping in. It's a beautiful concept and a lovely approach, though .. but one that's at its most effective on projects and tasks that are more than just a few code lines - in other words, it's at its most effective on those larger jobs which are the very ones that we don't do as practicals on a course because of all the time such an extended exercise would take.

However ... I have found good ways to "teach OO" - and indeed I have given two separate courses in the last week that have covered the subject. On Thursday, I was teaching OO in Perl and on Friday, OO in PHP. And - the week after next - I'll be teaching Perl for Larger Projects - a public course that covers the subject for a further group.

The Background to OO

Newcomers to OO start here

When you're programming, you're dealing with data - and probably a number of different types of data. And there are a certain number of things that you can do with each type of data.

For example, you could be writing a program that deals with animals. You can load in (somehow) the data relating to an animal. You can set its weight. You can read its weight. You can set and read its age. And if you know its breed and age, with appropriate background knowledge you can work out the proportion of the way it is through its expected life. But you can't (for example) work out its data transmission rate, nor know it' Mastercard number, as such things don't apply to animals.

In Object Oriented Programming, you'll write a series of named pieces of code that deal with each particular type of data - for an animal, following on with our example, you would write a piece of code that actually creates the necessary structures within your program to hold the data for an animal, named pieces of code to set values (if you chose not to set them only as you created the structures), and named pieces of code to get information back out. You might also write a piece of code to deal with closing down the structures when you were finished with them - if (for example) data changes stored only in memory had to be saved to your disc.

One of the great beauties of OO programming is that you can clearly define your specification for a data type ("class") and write all the code for that class in a separate file which can be tested, maintained and extended as a unit on its own away from the main application code. That means that your "animal code" can then be shared between a whole lot of different applications that relate to animals, and yet only the person who wrote the class needs to understand its internals. And all the people who use the code only need to understand how to call it, and what it returns.

Here's a sample Perl program - from Thursday - that USES a couple of classes, but without any knowledge of how they work internally.


use animal;
 
$rover = new animal("Rover",9,7);
$boots = new animal("Blue",14,6);
$george = new human("Joe the Plumber",42);
 
$boots->setage(12);
$boots->setfurcolour("grey");
 
@group = ($rover, $boots, $george) ;
 
foreach $life(@group) {
  $ea1 = $life -> geteage();
  $ag = $life -> getage();
  $a1 = $life -> getname();
  print "We have $a1 aged $ea1 in essence and $ag in truth\n";
}


And here are the results that produced:

Dorothy:ppcsrd08 grahamellis$ perl annie
We have Rover aged 63 in essence and 9 in truth
We have Blue aged 72 in essence and 12 in truth
We have Joe the Plumber aged 42 in essence and 42 in truth
Dorothy:ppcsrd08 grahamellis$


You'll notice that - as well as animals - my example has used a human. So I have two different types of objects in my list (@group). And yet when I call code such as geteage and getname there's only a single line involved, and not a big switch and case structure. This is because you're starting to see a further advantage of a well designed OO program - the ability for it to run a different piece of code depending on the class of object that's passed in. Or (describing that another way), if you have two geteage methods - one for a human and another for an animal - the program itself will be able to work out which one to use on each particular object it's called on. Neat, isn't it? This ability is known as Polymorphism.

Looking a little further behind the scenes, a human isn't all THAT different to an animal, and it would be a great shame to have to duplicate code. So in this example, our class of animal is used as the basis for our class of human, and in fact the only extra code I had to write for a human was that code which changes when I start looking at a human as a specific type of animal. In a larger example, this means that I've not got to duplicate thousands of lines of code, but rather I can write just a handful of new blocks of code to override those from the base class. In the OO lingo, this is known as inheritance.


If you would like to see a complete source code example in PHP, look here for a demonstration I wrote during yesterday.

Learning OO Programming

Hopefully, the article above has given you a little insight into the world of Object Orientation ... but there's far, far more to it than I can write in an article such as this, and it's something that you'll need to try out for yourself and learn; for some people "book learning" can be very effective, but for others a training course is a far easier way to get them over the initial hurdle. I suspect you can see the advert coming ... ;-)

We cover OO languages and principles behind them on a lot of our public courses:
Perl for Larger Projects
OO Programming in PHP
Lua Programming
Programming in Python
Ruby Programming
C++ Programming
These courses last from 1 to 3 days, and include other related subjects with the languages concerned as appropriate. By that, I mean that a language such as Python is ALL OO, so the subject is covered in our general Python course, but with a language such as PHP, OO is something that's not needed by everyone so we have a single extra day for that course, which you can take in addition to the general PHP course if you wish.

All public courses are run at our Melksham, Wiltshire, UK, hotel and training centre. Our current schedule may be found here We can also run private courses there (or on your site) if you wish.

For private courses, we can also cover Object Orientation in Java, and we can train you on [Incr-Tcl] - Tcl's OO extension.