Abstraction
Example
1.
You own a microwave oven. You know that it cooks and how to press the buttons.
Perhaps you own know a little of the physics involved. However, if presented
with the metals, plastic and whatnot needed to build one, you would have no hope
of building one. You are using abstraction.
Example
2.
You can trace Quicksort. You know that it has recursion. You know it is the
fastest in place sorting algorithm in most cases. However, if you had to code
it, you’d be in trouble. You are using abstraction.
Example
3.
You have a stack. You know that it can push and pop and return true if it is
empty. However, you have no idea how it does all of these things (and you
don’t really care). You are using abstraction.
As a user of objects, abstraction is your ideal. You should know what
the method does by reading the pre and post conditions and you don’t care how
the coder of the object actually did it.
Information
Hiding
When you have a team of programmers working on a project, you sometimes
want to keep your fellow programmers from messing with your section of your
code. You want them to only use your data in a very specific way.
Objects can do this for you.
Whatever you want people outside your class to be able to access, you
make public.
Whatever you don’t want people to access, make private.
Generally, all of the data is private and your fellow programmers must
use accessors and mutators to get to it.
Example: You are coding a rational
object.
Currently, you are storing the numerator and denominator as integers.
The denominator and numerator are public and everyone can read them.
This means your fellow programmers just write half.denominator=4 to
change the denominator.
Your boss comes to you and tells you that you have to get rid of the numerator and denominator and store the rational as a decimal.
All of your fellow programmer’s code that accesses the denominator or numerator must be rewritten as well as the class! That’s a pain.
Example 2: You are coding a rational object PROPERLY.
Currently, you are storing the numerator and denominator as integers.
The denominator and numerator are private so no one can read them except
things inside the class.
This means your fellow programmers can NOT just write half.denominator=4
to change the denominator, they must go through the mutator:
half.setdenominator(4) to do it.
Your boss comes to you and tells you that you have to get rid of the
numerator and denominator and store the rational as a decimal.
You must rewrite the class to do this but all of your fellow
programmer’s code that accesses does NOT need to be rewritten. Their
mutator call is still the same. It is just the code behind the mutator that
has changed. That’s great.
Because you are hiding the information in the object (hence the name: information
hiding),
you can change how the object is implemented without disturbing the users of the
object.
Section 7 - Questions
How are public properties different from private ones?
Why is abstraction important?
What is information hiding?
How does an accessor method aid information hiding?
Consider this nonsense class:
public
class Nonsense
{
private int n;
public Nonsense()
{
n = 0; }
public
void f()
{
n = n + 1; }
public
String k()
{
return "The number is " & n & "."
}
public
void g()
{
f();
n = 2 * n;
}
public
int h()
{return
n;
}
a) What is the name of the constructor? What is the name of the mutator? What is the name of the inspectors?