Access Privileges


One of the powerful features of Java is the ability to specify the access to fields and methods . For instance, let's say you develop a scientific application that has proprietary data used as part of the analysis. For obvious reasons you do not want this data freely accessible by anyone who uses the classes that make up the application. With Java, you can specify the access to this data so it is available by the application internally but not to any external entity.

There are four types of access, public , protected , private , and default. A top-level class can have public or default access. A public class is accessible anywhere inside or outside of the package in which it is defined. The default access is assigned if no other access modifier is specified. If no access modifier is specified, the class and its members are accessible only from the package in which the class is defined.

In addition to public and default access, an inner class can also have private or protected access. A private inner class is available only to the class in which it is defined. A protected inner class is accessible to the class in which it is defined and to subclasses. Table 7.1 summarizes the access types.

Table 7.1. Class Member Accessibility

M EMBER ACCESS TYPE

S AME CLASS

S AME PACKAGE

S UBCLASS, DIFFERENT PACKAGE

N ON-SUBCLASS, DIFFERENT PACKAGE

public

Yes

Yes

Yes

Yes

protected

Yes

Yes

Yes

No

private

Yes

No

No

No

default

Yes

Yes

No

No

A source file can contain any number of class declarations, but only one class in each file can be declared public . The name of the public class must be the same as the file name. For example, a public class named Foo would be contained in a source file named Foo.java .



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net