Java Reference
In-Depth Information
An example of an abstract class is java.awt.Component , the superclass of graphical user
interface components. Because numerous components inherit from this class, it contains
methods and variables useful to each of them. However, there's no such thing as a
generic component that can be added to a user interface, so you would never need to cre-
ate a Component object in a program.
Abstract classes can contain anything a normal class can, including constructor methods,
because their subclasses might need to inherit the methods. Abstract classes also can
contain abstract methods, which are method signatures with no implementation. These
methods are implemented in subclasses of the abstract class. Abstract methods are
declared with the abstract modifier. You cannot declare an abstract method in a class
that isn't itself abstract. If an abstract class has nothing but abstract methods, you're bet-
ter off using an interface, as you see later today.
Packages
Using packages, as mentioned previously, is a way of organizing groups of classes. A
package contains any number of classes that are related in purpose, in scope, or by inher-
itance.
If your programs are small and use a limited number of classes, you might find that you
don't need to explore packages at all. But as you begin creating more sophisticated pro-
jects with many classes related to each other by inheritance, you might discover the ben-
efit of organizing them into packages.
Packages are useful for several broad reasons:
They enable you to organize your classes into units. Just as you have folders or
directories on your hard disk to organize your files and applications, packages
enable you to organize your classes into groups so that you use only what you need
for each program.
n
They reduce problems with conflicts about names. As the number of Java classes
grows, so does the likelihood that you'll use the same class name as another devel-
oper, opening up the possibility of naming clashes and error messages if you try to
integrate groups of classes into a single program. Packages provide a way to refer
specifically to the desired class, even if it shares a name with a class in another
package.
n
6
They enable you to protect classes, variables, and methods in larger ways than on a
class-by-class basis, as you learned today. You learn more about protections with
packages later.
n
Packages can be used to uniquely identify your work.
n
 
Search WWH ::




Custom Search