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

Class is an abstract representation for some particular type of object. It is often described as a plan or blueprint for an object, as opposed to the actual object, itself.

Variables/Attributes

The data located within an object are referred to as variables or attributes.

Methods

Methods are functions that manipulate the data associated with an object or perform some other operation relevant to the object.

Inheritance

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

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.

Object

Object is the most fundamental concept in Object-Oriented programming. It is a self-contained collection of data (variables) and an associated set of functions (methods) that can be used to access and manipulate those data.

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

The following code fragments show correspondences between specific O-O concepts and Java constructs. They are not intended as models of well-written Java code.

Class


class List {

}  // end List

Variables/Attributes


class List {

  public boolean empty, full;
  private int aList[];
  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 printList() {

  }  // end printList

  public boolean addItem (int i) {

  }  // end addItem

}  // end SortedList

Instantiation


List aList;

aList = new List();
or
List aList = new List();

Messages


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