Sunday, 5 August 2007

Java Language Fundamentals (Part 8)

Objects and Classes

Although we won’t launch into Object-Oriented theory now, we’ll quickly cover the basic idea of classes and objects.

A class is like a blueprint or template. An Object is a tangible item, something you could touch or see. The common example is a house. You have a blueprint of a house, but you don’t live in the blueprint. You have to construct a house object to live in. A Method is like a function or procedure. You use methods to do things with an object.

For example, if you wanted to model the concept of a car, you might like to (amongst others) declare these variables to hold some important data about cars in general. So you might declare:
int topSpeed;
int lowSpeed;
final int MAX_SPEED = 200;

Then if you wanted to make 2 cars, you would have to type:

int topSpeed1, topSpeed2;
int lowSpeed1, lowSpeed2;
final int MAX_SPEED = 200;

As you can see, this would be very impractical for lot’s of cars! A better idea is to create a class.

For example:
class TheCar{int topSpeed;int lowSpeed;final int MAX_SPEED = 200;}

No comments: