15-212 Section A Notes

February 4, 1998
Thomas Kang

1. Administrative Details

As soon as homework #1 is ready to be returned, I'll post to the newsgroup and also note it in the announcements section. We plan to have it ready by tomorrow. We'll also try to have homework #2 done by Monday, so that you can look them over before the midterm on Tuesday.

There were several of you asking about pairing up with someone from a different section for your programming partner. Currently, we are very hesitant to do this. Here's the deal: if you can find another pair of people from the same two sections who want to partner up, then we'll let you do it. For example, let's say you're in section A and you want to work with someone in section B. If you can find another person in section A who wants to work with someone in section B, then choose a representative among you four to send an e-mail to the six people involved: in this example, the two pairs of partners and the section A and B TAs.

2. OOP Part I: Encapsulation

The concept that lies at the heart of object-oriented programming (OOP) is that of encapsulation -- basically, logically grouping all related data and functions into a single object. There are many reasons for doing this:

  • it organizes the code in logical chunks, making it more manageable;
  • it localizes the effects of code modification;
  • it allows for code re-use and sharing; and
  • it allows one to hide the source code.
The mechanism by which Java implements encapsulation is through classes. These classes are very similar to those of C++, but they are much more prevalent. In fact, everything in Java must belong to a class, including the main() function. Classes contain two type of information: member data and member methods. Data are values, such as ints and Strings; methods are functions.

These are the keywords in Java that are directly related to encapsulation: class, interface, public, protected, package, private, static.

There are two levels of access protection that one can designate for objects. One can declare a protection scheme for the entire class, and one can declare a protection scheme for each member in the class. There are four protection schemes in Java:

  • public (class, member)
  • protected (member)
  • package (class, member)
  • private (member)
public classes are visible to every other class (but not necessarily everything inside it!). public members within a class are likewise visible to every other class.

protected members are visible to other members in that class and to any of its subclasses formed by inheritance.

package is the default protection level given to those classes and members that do not have an explicit protection designation. (Therefore, you never explicitly write "package" to designate that protection scheme; however, you do write "package" to accomplish something else, which we'll discuss some other time.) Basically, you can designate a bunch of different classes as belonging to some package, and then all those classes in that package can access each other's package-level information, but those classes that are not in that package cannot. If you know about friends in C++, this is basically a cleaner implementation of that feature. More on this in some future section.

private members are visible only to other members in that class.

3. Midterm Review

Adam Berger, the section B TA, wrote up some nice review problems that you should try out in preparing for Tuesday's midterm. Also, I highly recommend that you try to do homework #3 before Tuesday; that will help you prepare for the exam, too. Finally, there will probably be some TAs who will move their office hours to Monday, so feel free to take advantage of that to ask any last-minute questions. I won't be changing my office hours, since it's before exam time anyway. I'll gladly entertain any literally last-minute questions that you may have then. :)