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 {
    //field and method declarations
}
The class body (the area between the curly brackets) contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the variables that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.

The first line of code plus the body of the class 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 implicit. 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 the most commonly used elements of a class declaration in the order they should or must appear. For a complete list, see the Java Language Specification (outside of the tutorial).

Class Declaration Elements
Element Function
@annotation (Optional) An annotation (sometimes called meta-data)
public (Optional) Class is publicly accessible
abstract (Optional) Class cannot be instantiated
final (Optional) Class cannot be subclassed
class NameOfClass Name of the class
<TypeVariables> (Optional) Comma-separated list of type variables
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). 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: default access (sometimes called package private), concrete (not abstract), extensible (not final) 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.

@annotation
Annotations provide a mechanism for annotating program elements with meta-data. Some annotations affect how many warnings the compiler issues; some annotations are used by other tools and represents data auxiliary to the program. For more information, see the Annotations (in the Learning the Java Language trail) section.
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 Creating and Using Packages (in the Learning the Java Language trail) section 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 Writing Abstract Classes and Methods (in the Learning the Java Language trail) section.
final
The final modifier declares that the class cannot be subclassed. The Writing Final Classes and Methods (in the Learning the Java Language trail) section 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.
<TypeVariables>
Used when declaring a generic class. Generic classes are mostly used when creating general purpose library classes. This affects how the class is used as we saw with java.util.ArrayList in the Stack example. For now, you shouldn't worry too much about this as it is covered in more detail in Generics (in the Learning the Java Language trail).
extends Super
The extends clause identifies Super as the superclass of the class, thereby inserting the class within the class hierarchy. The Managing Inheritance (in the Learning the Java Language trail) section 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 Creating and Using Interfaces (in the Learning the Java Language trail) section.

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.