Java Reference
In-Depth Information
java.lang contains the Object class, and the org.omg.CORBA package con-
tains an interface named Object . These two names collide if you use import
org.omg.CORBA.* ,inwhich case the compiler issues a warning. The solu-
tion is to use the fully qualified names whenever a name collision occurs. In
other words, if you import org.omg.CORBA.* and need to refer to a plain
java.lang.Object , then refer to it as java.lang.Object and refer to
CORBA objects as org.omg.CORBA.Object .
C/C
programmers new to Java usually assume that an import statement
actually brings code into the class bytecode files in a manner similar to that of
include files in C/C
++
.However, that is not the case. An import statement
simply provides an address where the compiler can look for class definitions.
++
5.3.3 Access rules
Access or visibility rules determine whether a method or a variable can be
accessed by another method in another class or subclass. We have used the pub-
lic modifier frequently in class definitions. It makes classes, methods, or data
available to any other method in any other class or subclass in any package.
Java provides four access levels:
public - access by any other class, anywhere.
protected - accessible by classes in the same package and by any subclasses of those
classes whether in the same package or in other packages.
Default (also known as “package private”) - accessible to classes in the same package
but not by classes in other packages, even if they are subclasses of classes in the package.
private - accessible only within the class. Even methods in subclasses in the same
package do not have access.
These access rules allow one to control the degree of encapsulation of classes.
Forexample, you may distribute your class files to other users who can make
subclasses of them. By making some of your data and methods private ,you
can prevent the subclass code from interfering with the internal workings of your
classes. Also, if you distribute new versions of your classes, you can change or
eliminate private fields and methods without affecting the subclasses made from
your superclasses.
If you do not put your class into a package, it is placed into the “default
unnamed package.” For simple demonstration programs this usually suffices.
However, with more serious programs you should use packages since otherwise
any other class in the unnamed package has access to your class's fields and
methods that are not set to private.
5.4 The final modifier and constants
We already briefly mentioned the final modifier in Chapter 3. It indicates that
a data field cannot be modified. In the declaration
final double PI = 3.14;
Search WWH ::




Custom Search