Class Definition Syntax

At its simplest level, a class definition consists of the keyword class followed by the name of the class and a set of class members within curly braces. The class keyword may be preceded by modifier keywords and annotations (see Chapter 4). If the class extends another class, the class name is followed by the extends keyword and the name of the class being extended. If the class implements one or more interfaces then the class name or the extends clause is followed by the implements keyword and a comma-separated list of interface names. For example:

public class Integer extends Number implements Serializable, Comparable {
    // class members go here
}

Generic class declarations include additional syntax that is covered in Chapter 4.

Class declarations may include zero or more of the following modifiers:

public

A public class is visible to classes defined outside of its package. See Section 3.6 later in this chapter.

abstract

An abstract class is one whose implementation is incomplete and cannot be instantiated. Any class with one or more abstract methods must be declared abstract.

final

The final modifier specifies that the class may not be extended. Declaring a class final may enable the Java VM to optimize its methods.

strictfp

If a class is declared strictfp , all its methods behave as if they were declared strictfp. This rarely used modifier is discussed in Section 2.6 in Chapter 2.

A class cannot be both abstract and final. By convention, if a class ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.