Java Reference
In-Depth Information
The
MyLogger
class is also declared
final
, meaning that it cannot be subclassed. For
example, the following
InvalidLogger
class does not compile because it attempts to extend
MyLogger
:
public class InvalidLogger extends MyLogger {
public InvalidLogger(java.io.File dest) {
super(dest);
}
}
The compiler generates the following error:
InvalidLogger.java:1: cannot inherit from final MyLogger
public class InvalidLogger extends MyLogger {
^
1 error
Naming Convention for Final Variables
The naming conventions of Java specify that variable names of constants be in all
uppercase letters. For example:
final long TIME = new java.util.Date().getTime();
If the variable name is a compound word, use the underscore character to separate
the words. For example:
final String COMPANY_NAME = “Sybex”;
For more information on Java naming conventions, visit
http://java.sun.com/docs/
codeconv.
The
final
modifi er on a fi eld does not affect how the fi eld is inherited. A subclass still
inherits the fi eld in the same manner as a non-fi nal fi eld;
final
methods are also inherited in
the same manner as non-
final
methods. The only difference is that a
final
method cannot
be overridden in the child class.
Static methods can also be declared
final
, meaning they cannot be overridden. For
example, the following
MyStaticLogger
class declares a
final static
method named
logMessage
:
import java.io.File;
public class MyStaticLogger {
Search WWH ::
Custom Search