Total Pageviews

Wednesday, July 21, 2010

Object oriented programming

Often there are times when I find the problem falls in to one of those buckets that have clear mapping to concepts of OOPs. Without much of thought I jump in code and move on.

There are times when its not. Thats when I dive to fundamenatals again. I was asking myself did the object orineted programming exist even before the foraml object oriented language introduction ? Did go back in the time of software days and discussed with few of my friends and answer is YES. Look the various components of the Linux kernel for instance. By using the Structs and function pointers people did do OOP with C. So it is these concepts which had inherent advnatges were made explicit with the help of the explicit constructs in the object oriented programming.

Well thought of diving in and capturing the most essential concepts used in the OOP's and the value that they bring in.

Two key things OOPs is attempting to solve is bring programming closer to problem domain and ease the reuse of the code. When done properly, the approach leads to simpler, concrete, robust, flexible and modular programs.

Also note that basic OOP concepts such as Abstraction, Encapsulation, Inheritance and Polymorphism should not be viewed as all independent things. Instead they are all related. Both the inheritance and polymorphism build on the top of Abstraction and Encapsulation.

A. Abstraction : "Abstraction is the elimination of the irrelevant and the amplification of the essential," according to Robert C. Martin in "Designing Object-Oriented C++ Applications Using the Booch Method," ISBN 0-13-203837-4

Abstraction is something we see in our everyday life. Be it any device like Car, Alarm clock or mobile. There are only few essential things that one needs to know in order to use them. Other details are not relevant and hence abstracted away.

In programming abstraction could be data abstraction or control abstraction.

Miller’s law: humans can only keep 7 ± 2 things in their head at a time.

Key to managing complexity: abstraction.
An abstraction is a view or representation of an entity that includes only the attributes of significance in a particular context. Abstraction is about emphasis on what an object is or does rather than how it is represented or how it works.

While abstraction reduces complexity by hiding irrelevant detail, generalization reduces complexity by replacing multiple entities which perform similar functions with a single construct. Generalization is the broadening of application to encompass a larger domain of objects of the same or different type.

Value abstraction brings in:
1. Hides all irrelevant details and thus making the main program simpler to design, write and maintain when the system is quite complex.
2. Reuse the proven components across applications
3. Abstraction allows the method of the internal implementation to continuously improve without affecting the rest of the system.

B. Encapsulation : Generally viewed as brother of the abstraction. Abstraction is noble but to get its benefits it has to be enforced. Abstraction is way to achieve intended(positive) and encapsulation is prevent any things that could break the abstraction(negative). Encapsulation is mechanism of implementing the abstraction so that its benefits can be realized.

The purpose is to achieve potential for change: the internal mechanisms of the component can be improved without impact on other components, or the component can be replaced with a different one that supports the same public interface. Encapsulation also protects the integrity of the component, by preventing users from setting the internal data of the component into an invalid or inconsistent state. Another benefit of encapsulation is that it reduces system complexity and thus increases robustness, by limiting the interdependencies between software components.[

C. Inheritance : Just like abstraction is closely related with generalization, the inheritance is closely related with specialization. the specialization relationship is implemented using the principle called inheritance.

A new class can be defined in terms of “diffs” from another class. New class is called subclass or derived class; old class is called superclass or parent class.

A derived class inherits all the entities of its parent class, but this can be restricted by access controls. Details are language-specific. Subclasses can override methods and provide its own implementation that may differ from the superclass.

D. Polymorphism : Polymorphisms is a generic term that means 'many shapes'. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.

The primary usage of polymorphism is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time (this is called late binding or dynamic binding).

The different objects involved only need to present a compatible interface to the clients. In principle, the object types may be unrelated, but since they share a common interface, they are often implemented as subclasses of the same superclass.

Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Polymorphism is not the same as method overloading or method overriding.