- Forward


An Introduction to Information Hiding
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
The Process Thus Far
  1. Read the textual description
  2. Create an initial encapsulation
  3. Add a constructor
  4. Add a toString() method
  5. Add an equals() and/or compareTo() method
A Motivating Example
Back SMYC Forward
  • The Worry:
    • If we were to give a PictureFrame class with public attributes to someone they could use it improperly
  • Opportunities for Misuse:
    • Assigning negative values to the width or height
    • Not using the canonical form (e.g., portrait orientation in which the width is less than the height)
A General Principle
Back SMYC Forward
  • The Principle:
    • Hide the internal details of a component from all other components
  • The Rationale:
    • Prevents damage from errant external code
    • Makes components easier to understand/use
    • Simplifies modification and repair
    • Facilitates re-use
Information Hiding Guidelines
Back SMYC Forward
  • Steps:
    1. Make all attributes private
    2. Provide accessor (that allow external code to "see" the contents of attributes) where needed
    3. Provide mutator methods (that allow external code to "change" the contents of attributes) where needed
  • Conventions:
    • The names of basic accessors often begin with get (and are sometimes called "getters")
    • The names of basic mutators often begin with set (and are sometimes called "setters")
  • Immutability Revisited:
    • Classes without mutators are said to define immutable objects
"Immediate" Benefits of Information Hiding
Back SMYC Forward
  • Simplifies Modification and Repair:
    • Changing the implementation of a class won't impact the users of that class
  • An Example:
    • The developer of the PictureFrame class should be able to change the way the cost is calculated without it having any impact on a user of the class
"Immediate" Benefits of Information Hiding (cont.)
Back SMYC Forward
  • Improves Understanding and Re-Use:
    • Classes are easier to understand and re-use because users are shielded from the details
  • An Example:
    • A method that looks like a simple accessor might really perform calculations (e.g., calculateArea() has been renamed getVisibleArea() because there is no reason for the user to know whether the area is an attribute or a calulcated value)
An Example
Back SMYC Forward

An Improved PictureFrame Class

javaexamples/oopbasics/pictureframe/privateattributes/PictureFrame.java
 
Using Private Methods
Back SMYC Forward
  • Defined:
    • A private method can not be called outside of the class in which it is defined
  • What Use Are They?
    • They generally act as "helpers"
An Example (cont.)
Back SMYC Forward

An Even Better PictureFrame Class

javaexamples/oopbasics/pictureframe/privatemethods/PictureFrame.java
 
Encapsulation is NOT Information Hiding
Back SMYC Forward
  • Encapsulation:
    • The process of defining objects/classes in terms of both their attributes and their behaviors
  • Information Hiding:
    • Hiding the internal details of a component from other components
Another Important Term
Back SMYC Forward
  • An Observation:
    • We can distinguish between the interface for a class (what the user needs to know about the class) and its implementation (which includes a variety of details that the user need not know about)
  • Application Programmer's Interface (API):
    • The "public" portion of a class or group of classes
The Process Thus Far
Back SMYC Forward
  1. Read the textual description
  2. Create an initial encapsulation with private attributes
  3. Add a constructor
  4. Add a toString() method
  5. Add an equals() and/or compareTo() method
  6. Add necessary accessors
  7. Add necessary mutators
  8. Add useful private methods (i.e., helper methods)
There's Always More to Learn
Back -