Java Reference
In-Depth Information
is reminiscent of the C/C++ include preprocessor directive, their function is
not the same. An import statement does not include any new code; it only aids
name resolution. Secondly, the “ * ” can only be used at the end of the package
name; it is not a true wildcard in the regular expression sense of the word.
Thirdly, every class has what is in effect an implicit import java.lang.* so
that you don't need to put one there. References to String or System or other
core language classes can be made without the need for either the import
statement or the fully qualified name (except as described in the next
paragraph).
If you need to use two different classes that have the same name but come
from different packages, you will still need to refer to them by their full names;
import can't help you here. As an example of this, consider the two classes
java.util.Date and java.sql.Date (though with any luck you won't need
to refer to both of them within the same class).
3.4
E NCAPSULATION , I NHERITANCE , AND P OLYMORPHISM
The classic troika of OOP buzzwords is “encapsulation, inheritance, and
polymorphism.” How does Java do each of these things?
3.4.1
Encapsulation is the grouping of data and algorithms together into units, and
it's also about hiding implementation details that are not important to the users
of the unit. The basic unit of encapsulation is the class. All Java code exists in
classes. A class is declared with the class keyword (Example 3.24).
Encapsulation
Example 3.24 A sample Java class declaration that doesn't actually do anything useful
public class
Sample
{
private int id;
public void method()
{
System.out.println(id);
}
}
Search WWH ::




Custom Search