Following is the source code for the other package, p2. The two classes defined in p2
cover the other two conditions that are affected by access control. The first class, Protection2, is
a subclass of p1.Protection. This grants access to all of p1.Protection's variables except for
n_pri (because it is private) and n, the variable declared with the default protection. Remember,
the default only allows access from within the class or the package, not extra-package
subclasses. Finally, the class OtherPackage has access to only one variable, n_pub, which
was declared public.
This is file Protection2.java:
package p2;
class Protection2 extends p1.Protection {
Protection2() {
System.out.println("derived other package constructor");
//
class or package only
//
System.out.println("n = " + n);
//
class only
//
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
This is file OtherPackage.java:
package p2;
class OtherPackage {
OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("other package constructor");
//
class or package only
//
System.out.println("n = " + p.n);
//
class only
//
System.out.println("n_pri = " + p.n_pri);
//
class, subclass or package only
//
System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home