Article
Object Oriented Concepts in Java – Part 2
Class Packages
For much of Part One, we worked with a class called Tree. Now, while Tree isn't an especially original name for a class, it fits the class perfectly. The problem is that the same name might fit another class just as well, and you'll have a naming conflict on your hands. Such conflicts aren't too serious when you get to write all your own classes; however, when you need to bring in a set of classes that someone else wrote for use in your program, things can get messy.
Consider, for example, what would happen if you've designed all of the classes to handle the logic for a program that will track the sales of buttons for clothing. In such a case it would be natural to have a class called Button, but then your boss tells you he or she wants a nice, graphical user interface for the program. To your dismay, you find that the class built into Java for creating buttons on user interfaces is called (you guessed it) Button. How can this conflict be resolved without having to go back through your code and change every reference to your Button class?
Class packages to the rescue! Java provides class packages (usually called just 'packages') as a way of grouping together classes according to their purpose, the company that wrote them, or whatever other criteria you like. As long as you ensure that your Button class is not in the same package as Java's built-in Button class, you can use both classes in your program without any conflicts arising.
By default, classes you create reside in the default package, an unnamed package where all classes that are not assigned packages go. For most of your programs it is safe to leave your classes in the default package. All of Java's built-in classes as well as most of the classes you will find available on the Internet and from other software vendors are grouped into packages, so you usually don't have to worry about your classes' names clashing with those of other classes in the default package.
You will want to group your classes into packages if you intend to reuse them in future projects (where new class names may clash with those you want to reuse), or if you want to distribute them for use by other developers (where their class names may clash with your own). To place your class in a package, you simply have to give the name of the package on a line at the top of your file. The convention is to use "com." followed by the name of your company as your package name. For example, classes that we develop at SitePoint.com are grouped in the com.sitepoint package by adding the following line to the top of our java files:
package com.sitepoint;
Be aware that when a class that resides in a package is compiled, the class file will be placed in a directory based on the name of the package. For example, compiling Button.java that belongs to package com.sitepoint creates the Button.class file in the com/sitepoint/ subdirectory of the current directory. To run or otherwise make use of a class in such a package, you should refer to it as if it were in the directory that contains the com subdirectory. So, to run the com.sitepoint.MyProgram class, you should go to the directory containing com (which contains sitepoint, which in turn contains the MyProgram.class file) and type:
C:\javadev> java com.sitepoint.MyProgram
Java will automatically look for com/sitepoint/MyProgram.class.
As it turns out, the Button class built into Java is actually in the java.awt package, which also contains all of the other classes for creating basic graphical user interfaces in Java (AWT stands for Abstract Windowing Toolkit, in case you were wondering). Thus, the fully qualified name of Java's Button class is java.awt.Button. To make use of this class without your program thinking that you're referring to your own Button class, you can use this full name instead. For example:
// Create a Java Button
java.awt.Button b = new java.awt.Button();
In fact, Java requires that you use the full name of any class that is not in the same package as the current class!
But what if your program doesn't have a Button class to clash with the one built into Java? Spelling out the full class name every time means a lot of extra typing. To save yourself this annoyance, you can import the class into the current package by putting the following line at the top of your .java file (just below the package line, if any):
import java.awt.Button;
Once it's imported, you can use the class by its short name (Button) as if it were part of the same package as your class.
Another convenient feature allows you to import an entire class package into the current package. This comes in handy again when creating user interfaces for your program, because to create a decent interface you might easily have to use a dozen or more classes from the java.awt package, and listing each by name on a separate import line can become as tedious as typing the full name of the class in your code. To import the entire java.awt package for use in a class without having to type their full names, you can add the following line to the top of the file:
import java.awt.*;
The java.lang package, which contains all the most basic classes of the Java language (e.g. the System class, which we have been using in the form of the System.out.println() method to display text on the screen), is automatically imported into every Java file automatically.
Before you can import or use the fully qualified name of a class to access it from another package, it must be declared public. Classes, by default, are only available for use by code in the same package. Obviously, this is not a desirable limitation if you are planning to distribute your code for use by others, or reuse your classes in multiple projects; therefore, any class that you foresee being useful to code outside of the class' package should be made public. Doing this is as simple as adding the keyword public to the very start of the class declaration. For example, it would make sense to declare our Tree class public:
package com.sitepoint;
public class Tree {
...
}
The same goes for CoconutTree:
package com.sitepoint;
public class CoconutTree extends Tree {
...
}