Object-oriented programming fundamentals

2008-10-19

The following sections use the analogy of a cat, demonstrating how cats might compare to OOP concepts.

Objects

Think of a real-world object, such as a cat. A cat could be said to have properties (or states), such as name, age, and color; a cat also has behaviors such as sleeping, eating, and purring. In the world of OOP, objects also have properties and behaviors. Using object-oriented techniques, you can model a real-world object (such as a cat) or a more abstract object (such as a chemical process).

What Is an Object?
Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.
A circle with an inner circle filled with items, surrounded by gray wedges representing methods that allow access to the inner circle.
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 (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). 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 picture of an object, with bibycle methods and instance variables.
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:
  1. 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.
  2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
  3. 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.
  4. Pluggability 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.


Instances and class members

Continuing with the real-world analogy of a cat, consider that there are cats of different colors, ages, and names, with different ways of eating and purring. But despite their individual differences, all cats are members of the same category, or in OOP terms, the same class: the class of cats. In OOP terminology, each individual cat is said to be an instance of the Cat class.

Likewise in OOP, a class defines a blueprint for a type of object. The characteristics and behaviors that belong to a class are jointly referred to as members of that class. The characteristics (in the cat example, the name, age, and color) are called properties of the class and are represented as variables; the behaviors (play, sleep) are called methods of the class and are represented as functions.

Inheritance
One of the primary benefits of OOP is that you can create subclasses of (or extend) a class; the subclass then inherits all the properties and methods of the class. The subclass typically defines additional methods and properties or overrides methods or properties defined in the superclass. Subclasses can also override (provide their own definitions for) methods defined in a superclass.

One of the major benefits of using a superclass/subclass structure is that it is easier to reuse similar code between various classes. For example, you could build a superclass called Animal, which contains common characteristics and behaviors of all animals. Next you could build several subclasses that inherit from the Animal superclass and add characteristics and behaviors specific to that type of animal.

You might create a Cat class that inherits from another class. For example, you might create a Mammal class that defines certain properties and behaviors common to all mammals. You could then create a Cat subclass that extends the Mammal class. Another subclass, say, the Siamese class, could extend (subclass) the Cat class, and so on.

Writing subclasses lets you reuse code. Instead of recreating all the code common to both classes, you can simply extend an existing class.

TIP
In a complex application, determining how to structure the hierarchy of your classes is an important part of the design process. Make sure you determine this hierarchy before you begin to program.

Interfaces
Interfaces in OOP can be described as templates of class definitions, and classes that implement interfaces are required to implement that template of methods. Using the cat analogy, an interface is similar to a blueprint of a cat: the blueprint tells you which parts you need, but not necessarily how those parts are assembled, or how the parts work.

You can use interfaces to add structure and ease of maintenance to your applications. Because ActionScript 2.0 supports extending only from a single superclass, you can use interfaces as a form of limited multiple inheritance.

You can also think of an interface as a "programming contract" that you can use to enforce relationships between otherwise unrelated classes. For example, suppose you are working with a team of programmers, each of whom is working on a different part (class) of the same application. While designing the application, you agree on a set of methods that the different classes use to communicate. So you create an interface that declares these methods, their parameters, and their return types. Any class that implements this interface must provide definitions for those methods; otherwise, a compiler error results.

Encapsulation

In elegant object-oriented design, objects are seen as "black boxes" that contain, or encapsulate, functionality. A programmer should be able to interact with an object by knowing only its properties, methods, and events (its programming interface), without knowing the details of its implementation. This approach enables programmers to think at higher levels of abstraction and provides an organizing framework for building complex systems.

Encapsulation is why ActionScript 2.0 includes, for example, member access control, so details of the implementation can be made private and invisible to code outside an object. The code outside the object is forced to interact with the object's programming interface rather than with the implementation details (which can be hidden in private methods and properties). This approach provides some important benefits; for example, it lets the creator of the object change the object's implementation without requiring any changes to code outside of the object--that is, as long as the programming interface doesn't change.

Polymorphism

OOP lets you express differences between individual classes using a technique called polymorphism, by which classes can override methods of their superclasses and define specialized implementations of those methods. In Flash, subclasses can define specialized implementations of methods inherited from its superclass but cannot access the superclass's implementation as in other programming languages.

For example, you might start with a class called Mammal that has play() and sleep() methods. You then create Cat, Monkey, and Dog subclasses to extend the Mammal class. The subclasses override the play() method from the Mammal class to reflect the habits of those particular kinds of animals. Monkey implements the play() method to swing from trees; Cat implements the play() method to pounce at a ball of yarn; Dog implements the play() method to fetch a ball. Because the sleep() functionality is similar among the animals, you would use the superclass implementation.

Using polymorphism in an application

Object-oriented programming lets you express differences between individual classes using a technique called polymorphism, by which classes can override methods of their superclasses and define specialized implementations of those methods. Funny aspect Boys use the word friendship to start love, girls use the same word to end love. Same words but different attitude. This OOPS concept is called as polymorphism!!!

0 comments: