Object - Oriented Programming Concepts

Much has been made of the O-O concept. Some have claimed that it is the panacea for all software development ills; others claim that it is mostly hype. The truth probably lies somewhere in the middle. Effective O-O programming is different from conventional programming with iterative languages such as Pascal, C, and Basic. One of the breakthroughs comes from thinking of your program as a hierarchy of classes and methods, meaning multiple levels of tools, that you can assemble into larger and more powerful combinations. Thus, instead of writing long blocks of code to accomplish a task, you build several high-level tools that you can put together easily to accomplish the operation, then you build lower-level with which to build each high-level tool, etc., until you can write the lowest level tool in just a few statements.

Learning to program in this way is not something you can just sit down and do or memorize. It takes time. But, it is a way of thinking that, if you persist, will at some point just happen. You will wake up some day and realize that you are thinking about your programming in a different way. In the meantime, you can encourage that process by learning the basic concepts of O-O design and thinking about how you can use them in your program designs. And practice, practice, practice writing code. It also helps to study other people's code critically, noting constructs that are simple, powerful, and elegant versus those that are long, messy hacks.


Basic Terms and Concepts

Object-oriented programming will be described in terms of a half-dozen or so basic terms and concepts. Each will be presented pictorially, discussed briefly, and then described with respect to its Java implementation.

The reader should note that O-O terms are highly circular; that is, one term is often defined and best understood in relation to other terms. Consequently, the reader should read through the discussion, below, once to get a sense of the whole and then a second time, concentrating on the individual concepts.

Class and Object

Class is an abstract representation of a particular type of object. Object is a concrete instance of a class. A class is often described as a plan or blueprint for an object, as opposed to the actual object, itself.

Variables and Methods

Data located within an object are referred to as variables or attributes. Methods are functions within an object that manipulate the object's variables or perform an operation relevant to the object.

Instantiation

Instantiation is the process by which individual objects are created, in time and space, by the system. It uses the class description as a guide or "blueprint" to create individual instances, or objects, of that class type.

Inheritance

Inheritance is the property whereby one class extends another class by including additional methods and/or variables.

Messages

Objects communicate with one another by sending messages from a statement in one object to a method in another object. They are often requests for an object to perform some operation on the data stored within the object.


Java Implementation of Basic Terms and Concepts

For o-o concepts to be useful, they must be transformed into Java code. In the discussion that follows, code fragments are provided to illustrate how Java implements the basic o-o concepts discussed above.

The examples are based on the notion of a list data structure. Typical operations include adding, deleting, and finding items in the list.

Class


class List {
}  // end List

Variables/Attributes


class List {

  public boolean empty, full;
  private int listArray[];
  private int num_items;

}  // end List

Methods


class List {

  private int list[];
  private int num_items;

  public boolean addItem (int i) {
  }  // end addItem

  public boolean deleteItem (int i) {
  }  // end deleteItem 

  public boolean findItem (int i) {
  }  // end findItem 

}  // end List

Inheritance


class SortedList extends List{

  public boolean addItem (int i) {
  }  // end addItem

  public int nextItem (int i) {
  }  // end nextItem 

  public boolean printList() {
  }  // end printList

}  // end SortedList

Instantiation


List aList;
aList = new List();

or

List aList = new List();

Messages


aList.addItem(4);
or
aList.findItem(25)