Previous | Next | Trail Map | Writing Java Programs | Java Objects


Declaring a Class

In general, a class declaration look like this:
[ modifiers ] class ClassName [ extends SuperClassName ] [ implements InterfaceNames ] {
    . . .
    class body
    . . .
}
The items between [ and ] are optional. A class declaration defines the following aspects of the class: Of the items in a class declaration, only the class keyword and the class name are required. The others are optional. If you do not make an explicit declaration for the optional items, the Java compiler assumes certain defaults.

The class name must be a valid Java identifier (that is, it must be a string of letters that begins with a Unicode letter, that contains only Unicode letters and digits, and that is not the same as any Java keyword). By convention, class names begin with a capital letter.

So, the most simple class declaration you can write would look like this:

class ImaginaryNumber {
    . . .
}
This snippet of code simply declares a class named ImaginaryNumber. When you declare a class like this, the compiler assumes the following: So what does all of this mean? The following sections will shed some light on it for you.

Superclasses

The Object class sits at the top of the class hierarchy tree in the Java development environment. Every class, whether written by you, by the Java development team, or by someone else, in the Java system is a descendent (whether direct or indirect) of the Object class. The Object class defines the basic state and behaviours that all objects must have such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class. For more information about the Object class, see missing page.

In Java, every class must have a superclass. If you do not specify a superclass for your class, it is assumed to be the Object class. To specify an object's superclass explicitly, put the keyword extends followed by the superclass name directly after the name of the class that you are declaring. So this class declaration

class ImaginaryNumber extends Number {
    . . .
}
explicitly declares that the Number class is the superclass of the ImaginaryNumber class. (The Number class is part of the java.lang package and is the base class for Integers, Floats and other numbers.)

Interfaces

When declaring a class, you can specify which, if any, interfaces that the class implements. So, what's an interface? An interface declares a set of methods and constants without specifying the implementation for any of the methods. When a class claims to implement an interface, it's claiming to provide implementations for all of the methods declared in the interface.

To declare that your class implements an interface, use the keyword implements followed by the interface. The implements clause follows the extends clause if there is one. If there is no extends clause, then the implements clause follows the class name. The implements clause is optional.

For example, imagine an interface named Arithmetic that defines methods named add(), subtract() and so on. Our ImaginaryNumber class can declare that it implements the Arithmetic interface, thereby guaranteeing that it provides implemenations for the add(), subtract() and other methods declared by the Arithmetic interface.

class ImaginaryNumber extends Number implements Arithmetic {
    . . .
    public Number add(Number) {
	. . .
    }
    public Number subtract(Number) {
	. . .
    }
}
Note that the method signatures of the methods declared in the Arithmetic interface and the method signatures of the methods implemented in the ImaginaryNumber class must match! [PENDING: is the above true even for access specifiers?] [PENDING: what are the access specifiers for methods defined in an interface].

Abstract Classes

To be somewhat circular, an abstract class is a class that has at least one abstract method in it. Well, that's not a very useful definition, is it? What's an abstract method? An abstract method is a method that has no implementation. The intent is to allow parent classes to leave some methods unimplemented but still require that subclasses (that are not also abstract) provide implementations for those methods.

Use an abstract class when your parent class knows that all of its subclasses will implement a certain method but each subclass will implement it differently. In other words, there is no default implementation that will satisfy any of the subclasses. For example, in a drawing application Rectangle, Triangle and other GraphicObject classes all implement the draw() method, but implement it differently; there is no appropriate default implementation for the draw() method. For more detailed information about abstract classes, see missing page.

To specify that your class is an abstract class, use the keyword abstract in the "modifiers clause" that appears before the class keyword.

abstract class GraphicObject {
    . . .
}
The compiler will print an error message if there are no abstract methods in a class you declare to be abstract.

Final Classes

You can declare a class to be final; a final class can have no subclasses. You would a class final to guarantee that other objects and methods using your class got exactly what they asked for. For example, the String class in the java.lang package is a final class. The Java system must guarantee that whenever a method or objects uses a String they get exactly a java.lang.String and not some string that is a really a subclass of java.lang.String. This ensures that the object in question has no strange, inconsistent, or unpredictable properties.

To specify that your class is a final class, use the keyword final in the "modifiers clause" that appears before the class keyword. For example, if you wanted to make the ImaginaryNumber class a final class, its declaration would look like this:

final class ImaginaryNumber extends Number implements Arithmetic {
    . . .
}
Note that it doesn't make sense for a class to be both final and abstract. In other words, a class that contains declared-but-unimplemented methods cannot be final. Attempting to declare a class as both final and abstract results in a compile-time error.

Public Classes

The last modifier that you can use in a class declaration is the public modifier. Use public to declare that the class can be used by objects outside the current package. By default classes can only be used within the package in which they are declared.

For example, the ImaginaryNumber class should be declared public because we want other classes and objects to have access to this class. Thus the class declaration for the ImaginaryNumber class now looks like this:

public final class ImaginaryNumber extends Number implements Arithmetic {
    . . .
}
By convention, when you use the public keyword in a class declaration, you should make it the very first item in a class declaration preceding either final or abstract if one of them is also being used.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents