Java Reference
In-Depth Information
In other object-oriented programming languages, such as C++, classes can have more
than one superclass, and they inherit combined variables and methods from all those
superclasses. This is called multiple inheritance , and it provides the means to create
classes that encompass just about any imaginable behavior. However, it significantly
complicates class definitions and the code needed to produce them. Java makes inheri-
tance simpler by allowing only single inheritance.
Interfaces
Single inheritance makes the relationship between classes and the functionality those
classes implement easier to understand and to design. However, it also can be restrictive,
especially when you have similar behavior that needs to be duplicated across different
branches of a class hierarchy. Java solves the problem of shared behavior by using inter-
faces.
An interface is a collection of methods that indicate a class has some behavior in addi-
tion to what it inherits from its superclasses. The methods included in an interface do not
define this behavior; that task is left for the classes that implement the interface.
For example, the Comparable interface contains a method that compares two objects of
the same class to see which one should appear first in a sorted list. Any class that imple-
ments this interface can determine the sorting order for objects of that class. This behav-
ior would not be available to the class without the interface.
You learn about interfaces during Day 6, “Packages, Interfaces, and Other Class
Features.”
Packages
Packages in Java are a way of grouping related classes and interfaces. Packages enable
groups of classes to be available only if they are needed and eliminate potential conflicts
among class names in different groups of classes.
By default, your Java classes have access only to the classes in the java.lang package,
which provide basic language features such as string handling. To use classes from any
other package, you must refer to them explicitly by package name or import them in
your source file.
To refer to a class within a package, you must normally use the full package name. For
example, because the Color class is contained in the java.awt package, you refer to it in
your programs with the notation java.awt.Color .
Search WWH ::




Custom Search