Sunday, June 22, 2008

Message passing

"The process by which an object sends data to another object or asks the other object to invoke a method." Also known to some programming languages as interfacing

Tuesday, May 27, 2008

Method

An object's abilities. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's methods. She may have other methods as well, for example sit() or eat(). Within the program, using a method should only affect one particular object; all Dogs can bark, but you need one particular dog to do the barking.

In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). Like a procedure in procedural programming languages, a method usually consists of a sequence of statements to perform an action, a set of input parameters to customize those actions, and possibly an output value (called return value) of some kind. The purpose of methods is to provide a mechanism for accessing (for both reading and writing) the private data stored in an object or a class.

Object

A particular instance of a class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that's defined in the object's class. Objects are key to understanding object-oriented technology. Examples of real-world objects: your dog, your desk, your television set, your bicycle.

Real-world objects have two characteristics: state and behavior. Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

A software object.

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields and exposes its behavior through methods . Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Consider a bicycle, for example:

A bicycle modeled as a software object.

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Bundling code into individual software objects provides a number of benefits, including:

  • Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
  • Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
  • Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
  • Plug-ability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Monday, May 26, 2008

Defining Classes and Subclasses

Classes form a hierarchy. For example, both letters and digits are characters that can be drawn on the computer screen. So, you can define

class ScreenCharacter {...}

and the subclasses

class Letter extends ScreenCharacter {...}

class Digits extends ScreenCharacter {...}

The keyword extends is used to distinguish the subclass (child class) and the superclass (parent class). If nothing is said about the superclass, then Java automatically considers it as a subclass of the Object class. So, with the above declarations you have constructed the following class hierarchy.The point is that all instance variables and methods that belong to the ScreenCharacter class are inherited by its subclasses so that they need not be defined again. For example, if at the higher level the ScreenCharacter class look like

class ScreenCharacter {

String name;

String fontname;

int fontsize;

int x, y;

void draw(Graphics g) {

g.setFont(new Font(fontname, Font.BOLD, fontsize));

g.drawString(name, x, y);

}

}

then the subclasses Letter and Digit do not have to introduce these instance variables and method again. By the way, if you are wondering why we chose the long name ScreenCharacter instead of Character, the reason is that the Java system already has a Character class built in (in the java.lang package) and we want to avoid name clashes.

The Letter class can be introduced as

class Letter extends ScreenCharacter {

String letterCase;

void setCase(String letterCase) {

this.letterCase = letterCase;

}

String getCase() {

return letterCase;

}

void toLowerCase() {

letterCase = "lowercase";

name = name.toLowerCase();

}

void toUpperCase() {

letterCase = "uppercase";

name = name.toUpperCase();

}

}

What distinguishes a letter from a character is that it is case sensitive: upper- and lowercase letters are possible. For digits case-sensitivity is not present. Above we have implemented the toUpperCase and toLowerCase methods in terms of methods with the same name in the String class.

Sunday, May 25, 2008

Object-oriented programming (OOP)

Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. It is based on several techniques, including inheritance, modularity, polymorphism, and encapsulation.