Classes


Plato would have approved of the concept of classes.

The easiest way to learn about classes is to step outside the domain of object-oriented programming for a moment and look at the real world. (Plato might not have approved of calling it "real.") We experience things in the world, and we create categories in our minds so that we can think about those things collectively. Figure 7.1 illustrates this mental process.

click to expand
Figure 7.1: Class as mental category

As another example, "dog" is a category. In daily life, when dealing with the external world, we don't really experience the category "dog." We experience individual dogs, such as Harley or Sumo or Rover. So "dog" is a class or category, and the individual dogs Harley and Sumo and Rover are individual instances of that class.

Now back to object-oriented programming. In Java, a class is a piece of code that describes a category of thing that you want to represent in software. In fact, a Java program is just a bunch of interacting class definitions. You might have suspected as much, since every complete application listing we have seen so far has contained the mysterious keyword class. On the other hand, you might not have suspected as much. So far we have put a lot of effort into concealing the object-oriented nature of class code, because it was not yet time to talk about objects and classes. Well, now the time has come.

Suppose you want to write a program to model the behavior of Harley, Sumo, and Rover. First you spend some time thinking about what these three have in common. Eventually you realize that they are all dogs, so you decide to create a class called Dog. (In Java, a class name can be any valid identifier, but by convention we capitalize the first letter.) You create the class by writing a source file that looks like this:

public class Dog {   . . . }

This is called a class definition. The code that goes between the curly brackets is the body of the class definition. Bodies can be as short as a few lines of code, for very simple classes. There is no upper limit on the size of the body, but typical large class bodies can be hundreds or even thousands of lines long. Of course, you don't yet know how to write a class body, but that is what the rest of this book is all about.

A class definition should appear in a file whose name matches the class name. So the Dog class should appear in a file called Dog.java. Compiling this file will result in a file called Dog.class. Now that you know what a class is, the .class filename extension makes sense. This is not an absolute rule, but explaining when you do and don't have to apply it would require presenting a number of concepts that are out of place here. If you are curious, please wait until Chapter 9, "Packages and Access."

So a class is something that you define when you write your source code. What about objects? An object is an individual instance of a class. Objects are created when your program is executed. More specifically, an object, like an array, is created by an invocation of the keyword new. The syntax for object creation is a bit different from the syntax for array creation, as you will see in the next section.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net