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 miniscule exposure to 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.



The first step in writing java programs, is to get a good reference on the language. Either a book, or online docs, whichever you prefer. But I think we should make sure you really understand classes, before writing your own programs.

First off, keep in mind that 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: "functions", "member functions", "methods" and probably a few more I forget. But I'll call them "member functions".


Sample Code!

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[]){}
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.


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 bolthole main page