A Quick and Dirty Intro to Classes in Java - Part 1

Motivating Classes via Data and Function Encapsulation

A stack data type might be defined in C as: Now suppose the user of my stack type (i.e. a programmer) wishes to write a function to clear a stack. Here are two possibilities:
	void clear(struct Stack *s) {
		while (!isEmpty(s))
			pop(s);
	}
i.e., repeatedly pop the stack until empty.

Here is a second (seemingly more clever) way of clearing the stack, taking into consideration the knowledge that the stack is being represented by an array and a pointer to the top element of the stack:

	void clear(struct Stack *s) {
		s->top = -1;
	}
i.e., assuming that top points to the top element of the stack (and having taken 22, assuming-- correctly as the case may be-- that an empty stack has a top of -1), the function simply assigns that value to top.