Tuesday, October 11, 2011

Object Oriented Programming - A Short Review

Before diving into the object oriented programming review lets us backup a bit.  In procedural programming a program is designed around functions or blocks of statements which manipulate data. These programs execute as a series of linear steps from start to finish. There is nothing wrong with this approach but procedural programs can be diffucult to reuse and to extend when programs grow bigger and more complex. One limitation of procedure programming is the tendency for large procedural-based programs to turn into "spaghetti code" which are difficult to read and maintain.

Object Oriented Programming allows us to write programs clearer and easier to understand by wrapping data and functionality inside of what is called an object. OOP is all about using objects. An object can be considered to be a "thing" that has some kind of state and can perform a set of activities that define the object's behavior.

So an object is just a software bundle of related state and behavior that is often used to model the real-world objcts that we find in everyday life.  Lets see a few examples... a Donkey have state(name, color, size, hungry) and behavior(running, walking, wagging tail). A Car have state(color, current speed, current gear, etc) and behavior(changing gears, applying brakes, etc). It is important to mention that in OOP we use abstractions to easily conceptualized real world objects into software objects.  Abstractions are one of the basic object oriented programming concepts. It is the mechanism that helps us reduce the complexity when modeling objects. The essence of abstraction is to extract essential properties while omitting inessential details. In the examples above, a Car doesn't need to have details of what kind of energy it will use to power the light beans, or where the Donkey gets its energy when he starts to run. These are inessential detail.


There are a three fundamental principles of OOP
Encapsulation
An object stores its state in fields and exposes its behavior through its methods. These methods operate on the object's internal state and they serve as the primary mechanism for object-to-object communication.
Each object's internal state and internal logic are hidden from other objects. In other words, the methods encapsulates the object's internal implementation.  Encapsulation allow us to separate an object's implementation from its behavior. And as long as the method  name remain the same, any changes to the internal implementation is transparent to the users.

Inheritance
Another important concept of object-oriented programming is inheritance.  Different kinds of objects often have a certain amount of common characteristics with each other.  For example, a super sport motorcycle, a dirt motorcycle, and a cruiser motorcycle share some of the same basic characteristics of motorcycles -size, weight, current speed, current gear, etc.  However, each has certain things that makes them unique from the other.  A super sport motorcycle has only one seat, where as, a cruiser usually have two seats.
Inheritance allows a class to inherit commonly used state and behavior from other classes and extend
or tailor that behavior to provide special action for specific needs.  When dealing with inheritance it is important to distinguish between a "is a" relationship and a "has a" relationship because objects can have both types of relationships with each other.  For example...

"A MotorcycleOwner is a Person and has a Motorcycle"
class MotocyleOwner extends Person { private Motorcycle motorcycle; ...}

The "has a" relationship is refers to class Composition (Client Relations).  Where as the "is a" relationship is refers to Inheritance (Subclass Relation).

Polymorphism
Polymorphism means the ability of an object to take many forms. One can briefly described it as "one interface, many implementations", polymorphism is a characteristic of being able to assign a different meaning  or usage to something in different contexts. There are two types of polymorphism:
Compile time polymorphism:  overloading of methods, which means that multiple methods with the same name but different lists of parameters. For example, a class that have multiple "print" methods with different parameters.
Runtime polymorphism: overriding of methods, which means that multiple methods with the same name that have the same parameter lists and the same return type. For example, a base class has a "print" method, and a subclass redefined its functionality by overriding the base class "print" method.

I tried to go over the basic features of OOP in little details. OOP is itself a very vast subject and going over every detail will take many blog posts :) However, the important thing to take from OOP is the ability to modify existing solutions to solve new problems. Object oriented programming allows us to reuse code. It allow us to "package" related data and related functions into objects that we can pass around or reuse in other systems without knowing how they work internally.

No comments:

Post a Comment