Session 4

Writing Classes and Methods

CS 2530
Intermediate Computing

Note: These notes are still incomplete, but they do give you an outline of the things we discussed today. I'll try to add detail over the next day or so. In the meantime, feel free to ask questions. You can always do that!

The Anatomy of a Java Class

Open up DefaultMemoDatabase again. Let's understand everything about this code we possibly can, after only a couple of weeks learning Java.

A class definition consists of

The class, its methods, and its instance variables are all usually labeled with access modifiers. The primary access modifiers will use are public and private. An item declared public can be accessed by any piece of code that knows the name of the item. An item declared private can be accessed only by code within the same class.

... using a class: create an instance. We have seen the Java operator for this: new

... other Java features:

... OOP features:

We will be talking more about instance variables throughout the course.

The Anatomy of a Java Method

A method:

... using a method: send a message to an object.
Pass arguments, if required.

... Java features:

... on-line documentation at

Java does have primitive data types, such as integers, real numbers, and characters. We just haven't used one yet! Actually, we have used integers -- literal constants passed as arguments to a built-in class's method. But we have not declared or used our own yet. In OO programming, most of the code will deal with objects. We won't see primitive types until we get to the most concrete implementations. (More later.)

Exercise: Private Parts

Classes are usually public, because we want to create instances of the class, add them to our program, and let them help solve the problem.

Methods are often public, because they correspond to the messages that other objects send to the instance.

Instance variables are private, because we want the instance to have exclusive control over changes to their values.

What about the other combinations? Might we have...

You might declare a class to be private so that no one else can create instances of it. For example, I might create a BinaryTree class for clients to use. A BinaryTree might contain instances of a TreeNode class, but clients should not create their own instances. They shouldn't even know they exist.

You might want to declare a method to be private because it is not a part of the object's public interface. Perhaps yopu create it as a helper method in your implementation.

You might want to declare an instance variable to be public for your convenience as a programmer. Sometimes it's nice to be able to reach into a class and access one of its instance variables. But...

Instance variables are always implementation details. Most methods that are not part of the object's public interface, such as helper methods, are implementation details, too.

If you make an implementation detail public, then other code can access it. The history of programming suggests that someone surely will. Many programmers consider public accessibility as a license for use. (My experience with Smalltalk...)

Any time other code knows about an implementation detail, changing the code becomes more difficult, because the changes affect other pieces of code.

"But what if I am the only programmer who uses the detail in outside code?" You have just made your own code harder to change.

"But this code will never change." This statement reveals a lack of imagination. Or experience.

Wrap Up

Eugene Wallingford ..... ..... August 30, 2012