Phils quick-n-dirty intro to object-oriented thinking

The following was originally a letter to a friend. It is currently rather rough. But it may serve someone some use, so I hereby share it. It assumes you have SOME exposure to programming before (ideally in java and/or C), on the level of having looked at the book once or twice, and being very confused :-) It is also heavily biased towards java's idea of classes, because it uses java as the example language. However, the concepts are easily transferrable to any other object-orientent language.

If you don't care about "Object Oriented" jargon, and have never done any programming at all before, you might be more interested in my Java tutorial instead. It focuses on just learning the basics of java, rather than jargon.


Lesson plan

  1. The basics -- being objectionable (this page)
  2. A lot of static: stick around
  3. When to use O-O methods
  4. A detailed game-plan (writing a poker game)
  5. Game-plan summary
  6. (Unfinished Chapter)Object Inheritance

Why look at Object Oriented Programming

What's so great about Object Oriented Programming (OOP) ?

One of the major concepts in computer science is the concept of code "abstraction". The idea is to organize your code into discreet units, that are relatively self-contained, so that you can focus on a particular task, while looking at a particular section of code. This is in contrast to having to know about ALL the code at once, even when you are only looking at a very small part of it. As code for a program gets larger and larger, this becomes more difficult, until it reaches the "impossible" jstage when you have hundreds of thousands of lines of code.

OOP is a way of formalizing the organization of code into units. Once you have defined a particular area or concept as a specific "object", you can then relegate all code related to that area, into one unified code "object". From then on, any time you need to know anything about that area, you know you can find it by looking at that object.

For the experienced programmer, this may still sound identical to "group your functions into separate files". However, OOP goes one further, and ensures that particular types of data, can only be accessed through blessed "methods". This can potentially allow for enforced sanity checks and so forth, that are not possible if you allow unfettered access to certain data and routines.

Your first step: be prepared

I've tried to make this tutorial easy enough to understand, that you can read through it without needing java experience. But once it comes to experimenting with your own programs, you will need a full language reference.

If you choose to eventually continue and write deeper code in java, you might choose to buy a book on it, or look at the online Java API reference, whichever you prefer.

If your objected oriented language of choice is not java... I hope you will find this tutorial helpful anyway! Just be sure to read up on all the core classes in your language of choice , before writing your own programs. Otherwise, you may waste hours, if not days, re-inventing something that is already provided for you in a more efficient manner.

Keep in mind that in java, everything is done by Classes. A class is the smallest chunk of a program that can exist by itself. In java, you cannot do anything unless a class is defined to do it.

Things get DONE, by calling "functions" of the class. There are multiple names for this in OOP terminology: "functions", "member functions", "methods" and probably a few more I forget. But I'll call them "member functions" here.

Sample Code: "Hello world!"

The typical example code is a "hello world" program, that prints "hello world" to your terminal. Here's the java version of "hello world":

public class hello
{
	public static void main(String args[])
	{

		System.out.println("Hello world");
	}
}
In C, you would have the main() function all by itself. But remember, "class" is the smallest chunk of code that can exist in java. So we have to have the function defined as part of a "hello" class.
  public static void main(String args[]){}
in the above example, is a specially recognised function. In a stand-alone (non-applet) java program, it can be automatically recognized by your runtime as "here's where I start doing things"

Note that it has a "static" modifier. The meaning of "static" will be covered later.


Get some class

NORMALLY, you have to create an "instance" of a class, to do anything. A class by itself is just a template for how an instance of it will act. It's sort of like a mould. An instance is like making something with that mould.

Here's an example of actually doing something with an instance of a class.

	TestClass  instance;
	instance = new TestClass();
	instance.dosomething();
"TestClass" is the class name. We make "instance" an instance of TestClass; Note that first you have to declare the type of "instance". Then you actually mould it into something with the "new" operator. If you dont assign it something 'new', anything you try doing with it will be an error, apart from checking if it is null.

"new" is the only way to actually make a new instance of a class in java. [with a few evil exceptions, which I shall blithely ignore here]

Once you have made the new instance of TestClass, then you can call a member function from that instance.
dosomething() is assumed to be a member function of class TestClass.


Variables... the spice of life

Okay, you have member functions. But what are you going to do with them? Just print out messages?

Nope. You want to store information, and maybe manipulate it. For that, you need variables. Just as there are "member functions" that work on instances, there are "member variables" that are specific to each instance. Again, you have to declare an instance of a class to use them.

Below, we have one member variable, and two member functions.

  class simpleclass
  {
	int memberv;

	public void print()
	{
		System.out.println("memberv is " + memberv);
	}

	public void setmember(int newval)
	{
		memberv = newval;
	}
  }

And here's a sample usage of the above class:
   simpleclass S1;
   simpleclass S2;
   S1 = new simpleclass();
   S2 = new simpleclass();
   
   S1.setmember(1);
   S2.setmember(2);

   S1.print();
   S2.print();

Each instance of simpleclass has its own internal copy of the variable called "memberv". First, we called the "setmember" member function of each instance, to set the variable inside that particular instance of simpleclass. Then, we called the member function that prints out the value of the variable, for a particular instance of simpleclass.

When you call the member function of a class, of an instance of that class, It only changes (or looks at) the local data inside that particular instance. Which is why you call them by instance.member_function(), not by themselves.

I think this is a good place to end the first lesson :-)

Lemme know what questions you have.

[yes, this extends to all you other net.folks out there!]

(Want to go on to lesson 2?)


OOP Table of Contents --- phil@bolthole.com
Part of bolthole.com... Solaris tips ... ksh tutorial ... AWK Programming tutorial