Visibility
in Detailed Design |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
Block | Entity is (created and) accessible within a block of code |
Subprogram | Entity is accessible within a subprogram (e.g., local variables; parameters passed by value |
Referential | Visibility is temporarily provided to another entity (e.g., parameters passed by reference) |
Class/File | Entity is visible to all entities within a
class/file (e.g., private variables in Java;
static variables in C) |
Generalized | Entity is accessible to its class and all
extensions (e.g., protected variables in Java) |
Group | Entity is accessible to all members of a
pre-defined group (e.g., package visibility in
Java, COMMON blocks in FORTRAN) |
Complete | Entity is visible everywhere (e.g.,
public variables in Java; global variables) |
+
-
#
public int length;
public void paint(Graphics g){ }
public class String{ }
private int size;
private void recalculate(){ }
protected String title;
protected void layout(){ }
class Converters { }
An Example
package datahiding; /** * A Node (containing a String) in a Queue * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class Node { // These attributes have package visibility String value; Node next; }
package datahiding; /** * A Queue (of String objects) * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class Queue { private Node back, front; /** * Construct a new (empty) Queue */ public Queue() { front = null; } /** * Pop a String off of the front of this Queue * * @return The String on the front of this Queue */ public String pop() { String value; if (front != null) { value = front.value; front = front.next; } else { value = null; } return value; } /** * Push a String onto the back of this Queue * * @param s The String to push */ public void push(String s) { Node temp; temp = new Node(); temp.value = s; temp.next = null; if (front == null) { front = temp; } else { back.next = temp; } back = temp; } }
package datahiding; /** * An example that uses a simple Queue * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class Example { public static void main(String[] args) { Queue q; String poppedOff, userInput; q = new Queue(); q.push("I"); q.push("love"); q.push("CS349"); System.out.println("\n\nHere's what was on the Queue:\n"); while ((poppedOff = q.pop()) != null) { System.out.println(poppedOff); } } }
import datahiding.Node; /** * An example that illustrates package visibility * * Note: This class is not part of the example2 package. * As a result, Node.value is not visible to this * class (since it has package visibility). * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class WillNotCompile { Node n = new Node(); String s = n.value; }