20-CS-694 Advanced Programming Techniques Spring 2012
Packages and Visibility

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Package Creation, What is Visible

Instructions for all applets:

   All applets contain a 'Press Me' button. Click it.

Visibility rules from "Learning Java":
    Meaning of data modifiers:
  • default - only accessible within the package
  • private - only accessible in the class
  • protected - accessible to classes in package and subclasses outside package
  • public - always accessible
Can't override to get a more private method.
When all classes are in the same package:
  • A private variable is not visible from an object of its own class. But a default, protected, or public variable is visible from an object. This is true for static variables as well.
  • The above is true even if trying to access the variable from a subclass.
  • ///**** The following used to be true - no longer ****///
    However, a private member can be accessed as follows in a class containing variable:
       f() { return subclass.variable; }
    
  • This does not hold when there is a variable of the same name in the subclass.
  • Access to all privates is allowed from the subclass through functions.
When classes are in different packages:
  • A private is accessible through a public method across packages.
  • A default, protected, or private is not accessible across packages.

    1.   V1.java

    Class A has public, private, protected, and default members. It also has a static member and is in the same file as the class whose objects need to access its members. All of A's members are accessible, even private ones, except for the static member.
 
    2.   V2.java
B.java

    Class B is like class A above except it has been moved to another file in the same directory as the class whose objects need to call its members and it has been declared to be a public class. The only added restriction is that private members are not accessible.
 
    3.   V3.java
C.java

    Class C is like class A above except it has been moved to another file in the same directory as the class whose objects need to call its members and it has not been declared to be a public class. The only added restriction is that private members are not accessible. In other words, access rules are the same as for class B.
 
    4.   V4.java
D.java

    Class D is like class A above except it has been moved to another file in a subdirectory of the directory containing the class whose objects need to call its members and it has been declared to be a public class. The subdirectory is called mypackage. To compile from the root directory:
   javac mypackage/D.java
   javac V4.java
The first line of D.java is package mypackage;. The first line of V4.java is import mypackage.*;. Now the protected, private, and default members are not accessible.
 
    5.   V5.java
E.java

    Class E is like class A above except it has been moved to another file in a subdirectory of the directory containing the class whose objects need to call its members and it has not been declared to be a public class. The subdirectory is called mypackage. To compile from the root directory:
   javac mypackage/E.java
   javac V5.java
The first line of E.java is package mypackage;. The first line of V5.java is import mypackage.*;. Now nothing is accessible and an object of this class cannot even be created even though the constructor is public.
 
    6.   V1.java
D.java
I.java

    Two files are in a package. One file contains two classes, one of which is public and the other private. The other file contains a public class. This example checks visibility of data in all package classes. Observe the default accessible field of the non-public package class is visible through the public class, the default accessible field of the public class is not visible, method Ii of class public class I is not visible but Ii2 is.
 
    7.   V2.java
D.java
I.java

    Expands on the above. Should contain everything you always wanted to know about visibility.
 
    8.   V3.java
D.java
I.java

    How to create an application in a jar file:
  1. javac mypackage/*.java
  2. javac Visible.java
  3. jar cf visi.jar *.class mypackage/*.class
  4. create visibility.html:
    <applet code=Visible.class archive=visible.jar width=90 height=22></applet>
    
  5. rm -f mypackage/*.class
  6. rm -f *.class
  7. appletviewer visibility.html