Thank you for visiting Blazing Games

The importing and extending classes

When you get right down to it, a program consists of data with code that manipulates the data. The data is what we call variables. The code is the actual program. To keep a program more manageable, code is generally broken into a group of methods. Methods perform specific tasks on the data, and can be used at any time. The concept of a class takes this basic idea to the next level. It combines the variables and the methods into a single entity.

While combining the data and the methods that manipulate the data into a class is a good idea, the class goes a bit further than that. More than one copy of the same class can be used. Every copy of the class, which is known as an instance, is independent of all other instances.

Classes are used like glorified variables. Any classes within the same package (see last chapter for an explanation of packages) is known to the class you are writing. To use other classes, they need to be imported. To import a class, you use the format

The class name is the full path of the class. To include a group of classes, you can use the wildcard (*) character. It is important to understand that only the classes within that directory will be included. The wildcard character does not include any directories within the directory you are indicating.

With our demo, we imported two directories.

The applet directory is obviously required because we are writing an applet. The awt, which is short for Active Windows Toolkit, is needed as it has all the methods for dealing with the user interface.

To give classes even more power, and to further increase the amount of code re-use, classes can build on top of existing classes to extend them. This is done by simply telling the compiler that the class you are creating is extending another class, as follows:

Extended classes contain the entire class that is extended. New variables and methods can be added. You can also take an existing method and re-write it, causing the new version of a method to be used whenever that method is called. More information on classes, interfaces, and extending classes will be presented in a future chapter.

Previous Chapter 4 Contents Page 4 of 10 Book Contents Next

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2003-2006 Blazing Games Inc. All Rights Reserved