![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |
The class declaration component declares the name of the class along with other attributes such as the class's superclass, and whether the class is public, final, or abstract.At minimum, the class declaration must contain the
class
keyword 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 snippet declares a new class named ImaginaryNumber.class NameOfClass { . . . }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 abstract, final or public
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 the Object class (declared in java.lang). The previous declaration of the ImaginaryNumber class implicitly declared Object as the superclass of ImaginaryNumber because it did not explicitly declare ImaginaryNumber's superclass. For more information about the Object class, see The java.lang.Object Class.To specify an object's superclass explicitly, put the keyword
extends
plus 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 of ImaginaryNumber to be the Number class rather than the Object class. You would write:class NameOfClass extends SuperClassName { . . . }This 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.)class ImaginaryNumber extends Number { . . . }When you declare that Number is the superclass of ImaginaryNumber you are also declaring that ImaginaryNumber is the subclass of Number. A subclass inherits variables and methods and methods from its superclass. Inheritance is one of object-oriented programming's most powerful paradigms. Through inheritance you can reuse code many times and build a hierarchy of increasingly specialized classes.
Creating a subclass can be as simple as including the
extends
clause 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
implements
followed by a comma-delimited list of the interfaces implemented by your class.For example, imagine an interface named Arithmetic that defines methods named
add()
,subtract()
, and so on. The ImaginaryNumber class can declare that it implements the Arithmetic interface like this:thereby guaranteeing that it provides implemenations for theclass ImaginaryNumber extends Number implements Arithmetic { . . . }add()
,subtract()
and other methods declared by the Arithmetic interface.By convention, the
implements
clause follows theextends
clause if there is one.Note that the method signatures of the methods declared in the Arithmetic interface must match the method signatures of the methods implemented in the ImaginaryNumber class. This and other information about how to create and use interfaces is in Writing Abstract Classes and Methods.
Declaring a Final Class
Java allows you to 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.Security: One mechanism that hackers use to subvert systems is to create subclasses of a class and then substitute their class for the original. The subclass looks and feels like the original class but does vastly different things possibly causing damage or getting into private information. To prevent this kind of subversion, you can declare your class to be final and prevent any subclasses from being created. The String class in the java.lang package is a final class for just this reason. The String class class is so vital to the operation of the compiler and the interpreter that the Java system must guarantee that whenever a method or objects uses a String they get exactly a java.lang.String and not some other string. This ensures that all strings have no strange, inconsistent, undesirable or unpredictable properties.
If you try to compile a subclass of a final class, the compiler will print an error message and refuse to compile your program. In addition, the bytecode verifier ensures that the subversion is not taking place at the bytecode level by checking to make sure that a class is not a subclass of a final class.
Design: Another reason you may wish to declare a class as final are for object-oriented design reasons. You may think that your class is "perfect" or that, conceptually, your class should have no subclasses.
To specify that your class is a final class, use the keyword
final
before theclass
keyword in your class declaration. For example, if you wanted to declare the ImaginaryNumber class as final, its declaration would now 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 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 { . . . }Declaring a Public Class
The last modifier that you can use in a class declaration is thepublic
modifier. Usepublic
to declare 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.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 { . . . }public
keyword in a class declaration, you should make it the very first item preceding eitherfinal
orabstract
if one of them is also being used.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 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 comma-delimited list of the interfaces implemented by ClassName
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 (a non-final, non-public, non-abstract, subclass of Object that implements no interfaces).
![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |