Getting Started with Java - Objects and Classes
(Page 5 of 6 )
Object-oriented programming is modeled on the observation that in the physical world, objects are made up of many kinds of smaller objects.
However, the capability to combine objects is only one aspect of object-oriented programming. Another important feature is the use of classes.
A class is a template used to create an object. Every object created from the same class has similar, if not identical, features.
Classes embody all features of a particular set of objects. When you write a program in an object-oriented language, you don't define individual objects. Instead, you define classes used to create those objects.
For example, you could create a Modem class that describes the features of all computer telephone modems. Most modems have the following common features:
The Modem class serves as an abstract model for the concept of a modem. To actually have something concrete you can manipulate in a program, you must use the Modem class to create a Modem object. The process of creating an object from a class is called instantiation, which is why objects are also called instances.
A Modem class can be used to create many different Modem objects in a program, and each of these objects could have different features, such as the following:
Some are internal modems, and others are external modems.
Some use the COM1 port and others use the COM2 port.
Some have error control, and others don't.
Even with these differences, two Modem objects still have enough in common to be recognizable as related objects. Figure 1.1 shows a Modem class and several objects created from that template.

Figure 1.1 The Modem class and several Modem objects.
Object ReuseHere's another example: Using Java, you could create a class to represent all command buttons—clickable boxes that appear on windows, dialog boxes, and other parts of a program's graphical user interface.
When the CommandButton class is developed, it could define these features:
The text that identifies the button's purpose
The size of the button
Aspects of its appearance, such as whether it has a 3D shadow
The CommandButton class also could define how a button behaves, deciding the following things:
Whether the button needs a single click or a double-click to use
Whether it should ignore mouse clicks entirely
What it does when successfully clicked
After you define the CommandButton class, you can create instances of that button—in other words, CommandButton objects. The objects all take on the basic features of a clickable button as defined by the class, but each one could have a different appearance and slightly different behavior depending on what you need that object to do.
By creating a CommandButton class, you don't have to keep rewriting the code for each command button that you want to use in your programs. In addition, you can reuse the CommandButton class to create different kinds of buttons as you need them, both in this program and in others.
Note - One of Java's standard classes, javax.swing.JButton, encompasses all the functionality of this hypothetical CommandButton example and more. You get a chance to work with it during Day 9, "Working with Swing."
When you write a Java program, you design and construct a set of classes. When your program runs, objects are instantiated from those classes and used as needed. Your task as a Java programmer is to create the right set of classes to accomplish what your program needs to accomplish.
Fortunately, you don't have to start from scratch. The Java language includes hundreds of classes that implement most of the basic functionality you will need. These classes are called the Java 2 class library, and they are installed along with a development tool such as SDK 1.5.
When you're talking about using the Java language, you're actually talking about using this class library and some standard keywords and operators recognized by Java compilers.
The class library handles numerous tasks, such as mathematical functions, text handling, graphics, sound, user interaction, and networking. Working with these classes is no different than working with classes you create.
For complicated Java programs, you might create a whole set of new classes with defined interactions among them. These classes could be used to form your own class library for use in other programs.
Reuse is one of the fundamental benefits of object-oriented programming.
Next: Attributes and Behavior >>
More Java Articles
More By Sams Publishing
|
This article is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.
|
|