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.

Example: You are coding a rational object.

Example 2: You are coding a rational object PROPERLY.

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

  1. How are public properties different from private ones?

  2. Why is abstraction important?

  3. What is information hiding?

  4. How does an accessor method aid information hiding?

  5. 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?
    b)       Assuming the following code was in another form, what would be done by each of the following lines. If nothing or an error would occur, explain why there would be an error.
                               I.         int a;
                             II.         Nonsense e;
                           III.         Nonsense w = new nonsense();
                           IV.         w.f();
                             V.         w.k();
                           VI.         w.g();
                         VII.         a = w.h();
                       VIII.         w.f (3)
                          IX.         w.n = 5
    c)       Does the class Nonsense support information hiding? Explain your answer.
    d)       Add a mutator function that allows us to change n to whatever the user wants. (Public public void mutate (a as integer)