| CS 535: Object-Oriented Programming & Design |
|
---|
Fall Semester, 1997
Doc 6, Information Hiding
To Lecture Notes Index
San Diego State University -- This page last updated 23-Sep-97
Information Hiding - Physical and Logical
Physical information hiding
Physical information hiding is when a class has a field and there are accessor
methods, getX and setX, setting and getting the value of the field. It is clear
to everyone that there is a field named X in the class. The goal is just to
prevent any direct access to X from the outside. The extreme example is a
struct converted to a class by adding accessor methods. Physical information
hiding provides little or no help in isolating the effects of changes. If the
hidden field changes type than one usually ends up changing the accessor
methods to reflect the change in type.
class PhysicalHiding
{
private int a;
private int b;
public int getA()
{
return a;
}
public int getB()
{
return b;
}
public int setA( int newA )
{
a = newA;
}
public int setB( int newB )
{
b = newB;
}
}
Logical Information Hiding
Logical information hiding occurs when the class represents some abstraction.
This abstraction can be manipulated independent of its underlying
representation. Details are being hidden from the out side world. Examples are
integers and stacks. We use integers all the time without knowing any detail on
their implementation. Similarly we can use the operations pop and push without
knowing how the stack is implemented. Given the following interface to a Point
class is there any way to determine how it is implemented? Does it used polar
coordinates internally or not?
class
Point
public int getX()
public int getY()
public int setXY( int newX, int newY )
public int getRadiusVector()
public int getPolarAngle()
public int setRadiusVector( float newRadius)
public int setPolarAngle( float newAngle)