|
|
Java Objects |
In general, a class declaration look 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 ] { . . . class body . . . }Of the items in a class declaration, only the
- modifiers declares whether or not the class is abstract, final or public
- ClassName sets the name of the class you are declaring
- SuperClassName is the name of ClassName's superclass
- InterfaceNames is a list of all of the interfaces implemented by the ClassName
classkeyword 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:
This snippet of code simply declares a class namedclass ImaginaryNumber { . . . }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.
- the Object class is the super class of
ImaginaryNumberImaginaryNumberimplements no interfacesImaginaryNumberis not abstract, not final, and is not public.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
extendsfollowed by the superclass name directly after the name of the class that you are declaring. So this class declarationexplicitly declares that the Number class is the superclass of theclass ImaginaryNumber extends Number { . . . }ImaginaryNumberclass. (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
implementsfollowed by the interface. Theimplementsclause follows theextendsclause if there is one. If there is noextendsclause, then theimplementsclause follows the class name. Theimplementsclause 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 theadd(),subtract()and other methods declared by the Arithmetic interface.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].class ImaginaryNumber extends Number implements Arithmetic { . . . public Number add(Number) { . . . } public Number subtract(Number) { . . . } }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 thedraw()method. For more detailed information about abstract classes, see missing page.To specify that your class is an abstract class, use the keyword
abstractin the "modifiers clause" that appears before theclasskeyword.The compiler will print an error message if there are no abstract methods in a class you declare to be abstract.abstract class GraphicObject { . . . }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
finalin the "modifiers clause" that appears before theclasskeyword. For example, if you wanted to make the ImaginaryNumber class a final class, its declaration would look like this: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.final class ImaginaryNumber extends Number implements Arithmetic { . . . }Public Classes
The last modifier that you can use in a class declaration is thepublicmodifier. Usepublicto 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:
By convention, when you use thepublic final class ImaginaryNumber extends Number implements Arithmetic { . . . }publickeyword in a class declaration, you should make it the very first item in a class declaration preceding eitherfinalorabstractif one of them is also being used.
|
|
Table of Contents |