HTML CSS PHP PERL

Classes, Properties, and Methods

 
Previous Table of Contents Next

Classes, Properties, and Methods

Object-oriented programming comes with its own set of jargon. You will have to learn a little bit of it to keep up, but only a little.

The most basic concept in OOP is the class. A class is simply a generic description of something. In real life cars, apples, and cats are all types of classes. Not all cats are the same, but conceptually there's such a thing as a "generic cat": mammal, quadruped, carnivore, small ears, long tails, and they sleep a lot. OOP also has the concept of a subclass, which is a more refined concept of the generic classlike a housecat, felis cattus.

The next two terms go together: an object is an instance of the class. If I wanted a concrete, physical example of something from the housecat class my cat Kirby would suffice. There's only one of him, and he embodies all the properties and functionality of the cat class, but he's a real cat and not an ideal, imaginary cat.

Objects have two more terms associated with them: methods and properties. Object methods are things that the object does. Properties are things that describe the object. My housecat would have the methods sleep, pounce, and eat; his properties would be furry, gray, and wears a blue collar.

A Thought-class: Car

Now that you've got the terminology down let's design a simple Car class from scratch. This class won't be terribly practical, but it will exercise the OOP terminology you've learned.

You're not going to design your class to inherit from other classes (a Car might inherit properties and methods from a MotorVehicle class), and you're not expecting anyone to subclass your class, so you don't have to worry about inheritance.

The first thing to do is make a list of things that a car has. For simplicity's sake we'll make this an electric car that's always on. When you push the accelerator, it goes. So these are its properties:

  • A catchy model name

  • A Vehicle ID Number

  • How fast it's moving

Then, there are some things that a car can do. These are its methods:

  • Accelerate/Decelerate

  • Brake

The properties and methods of the car represent things that the user can see and do. The car itself might have properties that it knows internally, which are none of the user's business. These are properties too, but they're private properties:

  • Engine Speed (RPM)

Now that you've got the idea of a car (a class), how will it work? First you'll need to create a car to use. Each class has a special method called a constructor that creates an instance of the class:

  • New Car

So how do you use your class? First you'd invoke the new car method:

"Create me a car!"

And like magic you'd have a car. You could then give commands to your car:

"Car, accelerate."

"Car, tell me the current speed."

"Car, brake."

What if you decided to have a race? You'd have to create two cars and give each individual car commands. Each car would track, on its own, internal information like engine speed, how fast it's moving, and the Vehicle ID number.

    Previous Table of Contents Next
    © 2000- NIV