- Forward


Visibility
in Detailed Design


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • The Concept
  • Types of Visibility
  • Notation
  • Rules of Thumb
  • Visibility in Java
Visibility
Back SMYC Forward
  • The Concept:
    • The ability of one entity to "see" (i.e., have access to) another
  • Use:
    • Information Hiding
    • Reduce Coupling
  • Time Dependence:
    • In principle, visibility can change over time
Types of Visibility
Back SMYC Forward
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)
Types of Visibility (cont.)
Back SMYC Forward
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)
Notation
Back SMYC Forward
  • UML Notation:
    • Public +
    • Private -
    • Protected #
  • Other Notation:
    • Public rose_public
    • Private rose_private
    • Protected rose_protected
    • Implemented (or Package) rose_implemented
Rules of Thumb
Back SMYC Forward
  • Avoid the use of global variables
  • Instance variables (i.e., attributes) should be protected/private
  • Utility functions should be protected/private
Visibility in Java
Back SMYC Forward
  • Public
    • public int length;
    • public void paint(Graphics g){ }
    • public class String{ }
    Expand
  • Private
    • private int size;
    • private void recalculate(){ }
    Expand
  • Protected
    • protected String title;
    • protected void layout(){ }
    Expand
  • Package
    • class Converters { }
    Expand
Visibility in Java (cont.)
Back SMYC Forward

An Example

javaexamples/datahiding/Node.java
 
javaexamples/datahiding/Queue.java
 
javaexamples/datahiding/Example.java
 

javaexamples/datahiding/WillNotCompile.java

 

There's Always More to Learn
Back -