- Forward


Accessibility and Visibility
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Accessibility/Visibility
Back SMYC Forward
  • The Concept:
    • The ability of one entity to "see" (i.e., have direct access to) another
  • A Related Concept:
    • The lexical scope of a name binding is the part of the source code in which the name can refer to the entity
Kinds of Accessibility/Visibility
Back SMYC Forward
Block Entity is accessible within a block of code
Subprogram Entity is accessible within a subprogram
Class/Private Entity is accessible by all entities within a class
Generalized/Protected Entity is accessible in its class and all extensions
Complete/Public Entity is accessible everywhere

There are other types of accessibility/visibility as well, some of which we will encounter later.

Notation
Back SMYC Forward
  • UML Notation:
    • Complete/Public +
    • Generalized/Protected #
    • Class/Private -
  • Other Notation:
    • Complete/Public rose_public
    • Generalized/Protected rose_protected
    • Class/Private rose_private
Accessibility/Visibility in Java
Back SMYC Forward
accessibility_java
Example Declarations 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
A Common (Unrelated) Confusion
Back SMYC Forward
  • The Issue:
    • Static methods can't use non-static attributes
  • The Reason:
    • The static method can be called even if an object has not been instantiated. Hence, the static method can be called even if no such attribute exists.
  • Note:
    • This is not an accessibility/visibility issue
    • Suppose multiple objects had been created?
A Common (Unrelated) Confusion (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/StaticMethods1.java
 
Block Accessibility
Back SMYC Forward

An Example

javaexamples/oopbasics/BlockAccessibility1.java
 
Block Accessibility (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/BlockAccessibility2.java
 
Block Accessibility (cont.)
Back SMYC Forward
  • An Issue With try-catch Statements:
    • Variables declared in try-catch statements can often cause difficulties
  • The Reason:
    • There are two distinct blocks
Block Accessibility (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/TryCatch.java (Fragment: v1)
 
Block Accessibility (cont.)
Back SMYC Forward

A Similar Example

javaexamples/oopbasics/TryCatch.java (Fragment: v2)
 
Subprogram Accessibility/Visibility
Back SMYC Forward

An Example

javaexamples/oopbasics/SubprogramAccessibility1.java
 
Subprogram Accessibility/Visibility (cont.)
Back SMYC Forward

A Common Mistake (Revisited)

/** * Explicit Value Constructor (for non-negative weights) * * @param pounds The number of pounds (must be positive) * @param ounces The number of ounces (must be positive) */ public Weight(int pounds, int ounces) { Weight aWeight; aWeight = new Weight(pounds, ounces, true); } /** * Explicit Value Constructor * * @param pounds The number of pounds (must be positive) * @param ounces The number of ounces (must be positive) * @param positive true for a postive Weight, false for negative */ public Weight(int pounds, int ounces, boolean positive) { this.pounds = Math.abs(pounds); this.ounces = Math.abs(ounces); this.sign = 1; if (!positive) this.sign = -1; }
Private Accessibility
Back SMYC Forward

An Example

Suppose that the Signature class contains the following declaration:

private String email, name, saying, url;

What happens in the following application and why?

javaexamples/oopbasics/PrivateAccessibility1.java
 
Private Accessibility (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/Signature.java
 
javaexamples/oopbasics/PrivateAccessibility2.java
 
Generalized/Protected Accessibility
Back SMYC Forward

An Example

javaexamples/oopbasics/im2/Chirp.java
 
javaexamples/oopbasics/im2/ExpandedChirp.java
 
Generalized/Protected Accessibility (cont.)
Back SMYC Forward
  • Changing Accessibility/Visibility of Attributes:
    • A child class can declare variables with the same type and name as in a parent. This is called shadowing.
    • The child can change the accessibility/visibility.
  • Changing Accessibility/Visibility of Methods:
    • A child class can override a method in its parent and "increase" its accessibility/visibility (e.g., private in parent but public in child).
There's Always More to Learn
Back -