Class Types: (note, inner classes may have different capabilities since they are "members")
Here is an interesting page! A modifier matrix (although I do see some contradictions to other sources)! And then, the Java Language Specification 8.1.1 Class Modifiers
Tutorials also: Packages, Import, Interfaces, Implements
One of the most useful features of an object-oriented language is the ability to declare a class that defines how to use the class without having to declare the methods. This is useful when the implementation is going to be specific to each user, but each user should use the same methods.
Example: Suppose (just suppose) you wanted to have a bunch of graphics objects. Consider a Shapes Class
setPosition() method is declared public void since it can be implemented here for all
The class java.awt.Graphics is an abstract class:
see the API for class description
Methods are declared in the Graphics class, but the actual code to execute the method is done elsewhere (dependent on browser/machine etc.).
When a class contains an abstract method the class must be declared abstract.
However, not all methods in the abstract class need to be abstract... some methods you might want to be inherited by all
Abstract classes should generally not have private methods (why? ) nor can they have static methods. (private methods will compile though)
the SUN tutorial "Abstract Classes and Methods"Import
Class packages are loaded with the import keyword.
Inner Classes:
Java 1.1 provided some new "types" of classes: nested classes.
Why would you want them? and examples from the tutorial
You have seen these when you compile code and get classes with the $ in
the .class name. These are all addressed in Java in a Nutshell, Chapter 3 as well as in
the SUN tutorial
More pages of interest are Inner Classes and the tutorial questions (a local copy )
Also, in CoreJava, Vol1, Chapter 6: Interfaces and Inner Classes, "Are inner classes useful?" some excerpts
Packages in Java are a way of grouping together related classes and interfaces (dot (.) separated)
They
Then you define your classes. These classes (the ones declared with this same
package name) are grouped together. These other classes are usually located in separate source files
Packages can be further organized into a hierarchy:
Look into the Date.java file see
The classes in the java package are guaranteed to be available in any Java
implementation, and are the only classes guaranteed to be available across different platforms. Classes in other packages (sun, netscape) may be available
only for specific implementations.
By default, your Java classes have access to only the classes in java.lang
To use classes from any other package, you have to either
If two packages each have classes with the same name, you can't import them both.
Try it and see what happens
...
Also, you can only use * to import multiple classes in a package. In fact,
normally importing all classes is simpler, and if has no negative effect on
compile time or code size, so there is generally no reason not to do it.
(Remember that import is not the same as #include in C ...
java does dynamic class loading.)
The use of * cannot be used for multiple
packages, i.e., import java.* won't work
See CoreVolume 1, Chapter 4: Objects and Classes section on Packages for how the compiler and virtual machine locate packages (CLASSPATH) also the JavaWorld article. Interesting note: "It looks in the source files to see if the source is newer than the class. If so, the source file is recompiled automatically."
Files of a package
should be located in a subdirectory that matches the full package name.
package acme.beans;
For this package, you will now need to create a
directory structure (
Here's how to create the appropriate directory structure for the acme.beans
package below the current working directory.
Assuming a package name of "acme.beans" create directories for Java
source files and Java class files:
You might also be interested in these options for javac while
we are at it. In general, see http://www.ecst.csuchico.edu/javaAPI/tooldocs/ for options
on java "tools".
Suppose you wanted to compile a couple of .java files.
Oh, this reminds me. If you have a class that needs to instantiate
another class (and possibly import), make sure that the class that this
one will use has been compiled. Example
javac -d . NervousText06BeanInfo.java
For your information:
Note that the -d and -classpath options have independent
effects. The compiler reads only from the class path, and writes only
to the destination directory.
It is often useful for the destination directory to be on the class path.
If the -d option is not specified, the source files should be
stored in a directory hierarchy which reflects the package structure,
so that the resulting class files can be easily located.
So if a class is not defined as public, then only other classes in the same package can access it. This is often OK at first since you are defining all of your classes in the same directory (by defaults, same package).
For methods, you should generally specify if you want them to be public or private (like setting protocols (accessors, etc) in Smalltalk). Core2 suggests that data (cv and iv) always be private (to promote encapsulation)
Note: every source file can contain, at most, one public class, which must have the same name as the file.
Many books suggest *every* class be defined in a separate file supporting the
compiler convention that only one class should be located in each Java source file - actually the compiler cares only about every public class being
a separate file
The above is a common cause of compiler error - hard to find if you don't know
this convention!
O'Reilly course 3: Lessons 3 (packages), 5 (abstract classes), 7 (nested classes)
Packages
Declaration
java.awt.image.ColorModel
Import and Ambiguity
package java.util;
at the top. This identifies that it is in that package.
You might note
also that there is a java.sql.Date class in the package java.sql
Usually it is easier to
import java.util.*;
and then use
Date today = new Date();
Location, Compiling and Calling
/acme/beans) so the Java compiler can put the generated class files in
the proper locations. (The java compiler used to create
the directories upon compilation if you had not done so already. These days you need to use the -d option for it to do this (see below)))
mkdir -p ./src/acme/beans
mkdir -p ./classes/acme/beans
In case you are unfamiliar with the -p option (from the man pages):
-p With this option, mkdir creates dir by creating
all the non-existing parent directories first.
Compiling the code to get the correct structure:
javac -d . NervousText06.java // if the package name is acme.beans you will see ./acme/beans/NervousText06.class
OR javac -d . *.java
Scope: Remember, if you do not specify features as either private or public, then that feature (class, method, variable) can be accessed by all methods in the same package. (friendly)
javac -classpath .:/home/avh/classes:/usr/local/java/classes ...
javac -d /home/avh/classes MyProgram.java
causes the compiled class files for the classes in the
MyProgram.java source file to be saved in the directory
/home/avh/classes. If your classes are defined in the
package demos/awt, the class files would be placed in
the directory /home/avh/classes/demos/awt.