The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Declaring Classes

You've seen many class definitions of the following form:
class MyClass {
    //member variable and method declarations
}
The first line of code is called the class declaration. The preceding class declaration is a minimal class declaration; it contains only those components of a class declaration that are required. Certain aspects of this class, though unspecified, are assumed. The most important is that the direct superclass of MyClass is the Object class. You can provide more information about the class, such as the name of its superclass, whether it implements any interfaces, whether it can be subclassed, and so on, within the class declaration.

The next table shows all the possible elements of a class declaration in the order they should or must appear.

Class Declaration Elements
Element Function
public (Optional) Class is publicly accessible
abstract (Optional) Class cannot be instantiated
final (Optional) Class cannot be subclassed
class NameOfClass Name of the class
extends Super (Optional) Superclass of the class
implements Interfaces (Optional) Interfaces implemented by the class
{
    ClassBody
}
Provides the class's functionality
The right-hand side describes the purpose of each component. The required components are shown in boldface. All the other components are optional, and each appears on a line by itself within the table (thus, extends Super is a single component), but you don't have to write your code that way. Italic indicates an identifier, such as the name of a class or an interface. If you do not explicitly declare the optional items, the Java platform assumes certain defaults: a nonpublic, nonabstract, nonfinal subclass of Object that implements no interfaces.

The following list provides a few more details about each class declaration component. The list also provides references to this chapter's sections that talk about what each component means, how to use each, and how it affects your class, other classes, and your program.

public
The public modifier declares that the class can be used by any other class. Without the public modifier, your class can be used only by classes in the same package. Look in the section Creating and Using Packages (in the Learning the Java Language trail) for information.
abstract
The abstract modifier declares that the class cannot be instantiated. For a discussion about when abstract classes are appropriate and how to write them, see the section Writing Abstract Classes and Methods (in the Learning the Java Language trail).
final
The final modifier declares that the class cannot be subclassed. The section Writing Final Classes and Methods (in the Learning the Java Language trail) discusses the reasons for writing final classes.
class NameOfClass
The class keyword indicates to the compiler that this is a class declaration. The name of the class — NameOfClass — follows the class keyword.
extends Super
The extends clause identifies Super as the superclass of the class, thereby inserting the class within the class hierarchy. The section Managing Inheritance (in the Learning the Java Language trail) discusses the responsibilities and benefits of subclasses.
implements Interfaces
To declare that your class implements one or more interfaces, use the keyword implements followed by a comma-separated list of the names of the interfaces implemented by the class. Details about writing your own interfaces and how to use them can be found in the section Creating and Using Interfaces (in the Learning the Java Language trail).

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.