|
|
Objects, Classes, and Interfaces |
At minimum, a class declaration must contain theclasskeyword and the name of the class that you are defining. Thus the simplest class declaration that you can write looks like this:For example, this code declares a new class namedclass NameOfClass { . . . }ImaginaryNumber:Class names must be a legal Java identifier and, by convention, begin with a capital letter. Often, a minimal class declaration such as this one is all you'll need. However, the class declaration can say more about the class. More specifically, within the class declaration you can:class ImaginaryNumber { . . . }
- Declare what the class's superclass is.
- List the interfaces implemented by the class.
- Declare whether the class is public, abstract, or final.
Declaring a Class's Superclass
In Java, every class has a superclass. If you do not specify a superclass for your class, it is assumed to be theObjectclass (declared injava.lang). So the superclass ofImaginaryNumberisObjectbecause the declaration did not explicitly declare it to be something else. For information about theObjectclass, see TheObjectClass.To specify an object's superclass explicitly, put the keyword
extendsplus the name of the superclass between the name of the class that you are declaring and the curly brace that opens the class body, like this:For example, suppose that you wanted the superclass ofclass NameOfClass extends SuperClassName { . . . }ImaginaryNumberto be theNumberclass rather than theObjectclass. You would write:This explicitly declares that theclass ImaginaryNumber extends Number { . . . }Numberclass is the superclass ofImaginaryNumber. (TheNumberclass is part of thejava.langpackage and is the base class for theInteger,Float, and other subclasses.)Declaring that
Numberis the superclass ofImaginaryNumberimplicitly declares thatImaginaryNumberis the subclass ofNumber. A subclass inherits variables and methods from its superclass. Creating a subclass can be as simple as including theextendsclause in your class declaration. However, you usually have to make other provisions in your code when subclassing a class, such as overriding methods. For more information about creating subclasses, see Subclasses, Superclasses, and Inheritance.Listing the Interfaces Implemented by a Class
When declaring a class, you can specify which, if any, interfaces are implemented by the class. 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 one or more interfaces, use the keyword
implementsfollowed by a comma-delimited list of the interfaces implemented by your class. For example, imagine an interface namedArithmeticthat defines methods namedadd,subtract, and so on. TheImaginaryNumberclass can declare that it implements theArithmeticinterface like this:thereby guaranteeing that it provides implementations forclass ImaginaryNumber extends Number implements Arithmetic { . . . }add,subtractand other methods declared by theArithmeticinterface. If any implementations for methods defined inArithmeticare missing fromImaginaryNumber, the compiler will print an error message and refuse to compile your program:By convention, thenothing.java:5: class ImaginaryNumber must be declared abstract. It does not define java.lang.Number add(java.lang.Number, java.lang.Number) from interface Arithmetic. class ImaginaryNumber extends Number implements Arithmetic { ^implementsclause follows theextendsclause if there is one.Note that the method signatures of the methods declared in the
Arithmeticinterface must match the method signatures of the methods implemented in theImaginaryNumberclass. This and other information about how to create and use interfaces is in Creating and Using Interfaces.Public, Abstract, and Final Classes
You can use one of three modifiers in your class declarations to declare that your class is public, abstract, or final. The modifiers go before theclasskeyword and are optional.The
publicmodifier declares that the class can be used by objects outside the current package. By default a class can only be used by other classes in the same package in which it is declared. You'd probably like other classes and objects to be able to useImaginaryNumber, so it should be declared public:By convention, when you use thepublic class ImaginaryNumber extends Number implements Arithmetic { . . . }publickeyword in a class declaration, make it the very first item in the declaration.The
abstractmodifier declares that your class is an abstract class. An abstract class may contain abstract methods (methods with no implementation). Abstract classes are intended to be subclassed and cannot be instantiated. For a discussion about when abstract classes are appropriate and how to write them, see Writing Abstract Classes and Methods.When you use the
finalmodifier you declare that your class is final; that is, that your class cannot be subclassed. There are (at least) two reasons why you might want to do this: security reasons and design reasons. For further discussion of final classes, see Writing Final Classes and Methods.Note that it doesn't make sense for a class to be both final and abstract. In other words, a class that contains unimplemented methods cannot be final. Attempting to declare a class as both final and abstract results in a compile-time error.
Summary of a Class Declaration
In summary, a class declaration looks like this:The items between [ and ] are optional. A class declaration defines the following aspects of the class:[ modifiers ] class ClassName [ extends SuperClassName ] [ implements InterfaceNames ] { . . . }Of the items in a class declaration, only the
- modifiers declare whether the class is public, abstract, or final
- ClassName sets the name of the class you are declaring
- SuperClassName is the name of ClassName's superclass
- InterfaceNames is a comma-delimited list of the interfaces implemented by ClassName
classkeyword and the class name are required in a class declaration. The other items are optional. If you do not make an explicit declaration for the optional items, the Java compiler assumes certain defaults: a non-final, non-public, non-abstract, subclass ofObjectthat implements no interfaces.
|
|
Objects, Classes, and Interfaces |