This page is best viewed at a resolution
of 1024 x 768 or better
First, a little lesson on Object Oriented Programming in Java.
It is important to know the difference between a class and an object. Be careful, they are very closely related but very different. A class is the written code that defines what an object will be like, but it is NOT an object! It only defines an object. Think of a class as a kind of blueprint. For example, say you wanted to build a bicycle. First, you make instructions of exactly what the bike is made of, how the bike is supposed to be made and what it is supposed to do; these are the blueprints. With these blueprints you can build as many bicycles as you want and they will all be exactly the same since they were built using the same blueprints.
To do this in Java you would start by creating a class. The class is
the blueprint that holds all of the
information about the object you want to create. Once you
have that information, you can then make objects of that type.
To use the example above, if you wanted to make a bicycle object in Java, first you
need the blueprints which you code in "
class Bicycle". In this class,
you add all information
relevant to Bicycles, such as the number of wheels, how many
gears, coaster brakes or hand brakes and so on. You also
define operations that it can do, like pedal, brake, turn etc.
Now that you have the definition of exactly what a Bicycle is (the class), you
can create actual Bicycle objects. By saying
Bicycle myBicycle = new Bicycle();" you create an
object called myBicycle of type Bicycle.
Now lets say that you want a bicycle with an engine on it so that you don't have to pedal. You could make a whole new set of blueprints for the new bike, but you already have a perfectly good bicycle that you could just add an engine to. In this instance, you would take the existing blueprints for the bicycle and just add what it needs to support an engine. The original bicycle blueprints are still there, but now there is an additional blueprint that does everything that a regular bicycle does and then some.
This adding of extra functionality to a class is called extension in Java. The code to create the new class
would be "
class MotorBike extends Bicycle".
This says that we are creating a new class called
MotorBike that automatically inherits everything that Bicycle has. If Bicycle has
breaks, pedals or gears, MotorBike does too, by default, and it behaves exactly as if
it were a Bicycle. MotorBike is an exact copy of Bicycle that we can now add the engine to.
While the MotorBike can do everything a Bicycle can do, some of things that a Bicycle does need to be changed because of the new motor. For example, the pedals still perform the same function on the MotorBike as they did on the Bicycle, but now they need the added ability to disconnect when the motor is running. This is done by redefining new pedals in MotorBike that look exactly like the old pedals, but have the extra functionality. The idea is similar to extending a class, but when it is used on a member of a class it is called overriding. So now we have new pedals in MotorBike which override the pedals from Bicycle.
The tutorial presented here is taught in a top-down approach, so there may be things which are not immediately obvious. This is because they are discussed later in the tutorial. However, for things which are built in to Java, look at the Java Platform Specification.
Click here for Sun's Java tutorial.
To begin this tutorial, click any of the
links on the left or click here.