Program 6.1: A Web Site Class

Classes and objects are the primary distinguishing feature of OOP languages. A class is a user defined data type that can associate the methods which act on an object with the object itself. The earliest 3GLšs (Fortran, Basic) provided a few basic data types like real and double, and that is all you had to work with. In the second generation of 3GLšs (C and Pascal) programmers could create their own data types using structs or records. However these types were separate from the functions that acted on them. In OOP languages data and methods are both part of programmer defined classes.

Java provides a number of simple data types like int, float and String. However very often the data you want to work with is not an int, a float or a String. Classes let programmers define their own more complicated data types.

For instance suppose your program needs to keep a database of web sites. Each site has a name, a URL, and a description. In traditional programming languages you'd have three different String variables for each web site. With a class you combine these into one thing like this.

public class website {

  public String name;
  public String url;
  public String description;

}
These variables (name, url and description) are called the member variables or fields of the class. The fields tell you what a class is and what its properties are. Each of these variables in this example has been declared to be public. That means that any other object can use these variables.


Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home