Sunday, January 17, 2010

Principles of Object Oriented Programming

    Each Computer Program consists of code & data. When we organize a program in such fashion that it seems that code act on data. this is the premier way we used to program in C.This way is called procedural way but when size of code grows beyond a certain limit it becomes hard to manage.
     Second approach is to organize code in way that program consist of well thought out entities (read object) and each entity has it’s own attributes & method to manipulate that attribute it can be roughly called an Object Oriented programming(OOP).
   OOP equally govern by a common principle of code reuse.
 BASIC Principles of OOP
 There are three basic principles that can describe OOP as whole. Those principles are as follows.

  1) Abstraction: Abstraction can be thought out as an act of emulating a real world object in programming world.  E.g. we can define customer entity in programming world as Abstract representation of a customer in real life.
    Abstraction can be achieved by composition. In real world an object can further thought out as composed of many Constituent object.e.g. Car can be thought out as composition of engine, gearbox, steering, sound system etc. Looking at car as single object undoubtfully help to drive car better than thinking it as many object interacting with each other
 simultaneously. We can delve into inner object only when we need to do so until then we think of a car as single unit which provided us methods to drive by steering wheel, breaks to stop car, feel fuel in fuel tank to keep car running.
Abstraction can help us to keep complexity at minimums.

2) Encapsulation: Encapsulation can be thought out as an act of putting protective
wrapper around code & data in program so that client program can’t access the server programs code and data arbitrarily, but it should follow the well-defined Interface defined by server program. 
    This helps to keep server program’s code and data secure. Further it mask client program from the way actual Implementation of interface in server program.The server program can modify the Implementation of interface keeping same method of accessing it, the client Program doesn’t need any modification even if the server program change actual Implementation of interface in it.
   Encapsulation defines access rule (levels Public/private/protected/friend/internal/) for accessing Program’s Code & data Applicable to client program as well as it’s own constituent units.

3) Inheritance: Inheritance can be thought out as a process by which an object acquires the attributes, behaviors of another object being special case of it.
  E.g. each animal has common behaviors like eating food, breating,sleeping,speak,reproducing new offspring etc, attributes like ability to wander (plant can’t move on themselves) etc. Hen is special case of animal that inherits all these attribute & behaviors but it may contain its own attributes & behaviors that are not common to all animals e.g. it can fly. Inheritance helps to prevent un-necessary reductant coding.
 4) Polymorphism: It is common practice in philosophical text to start understanding a concept right from its linguistic (Poly-morph-ism) structuring. Polymorphisms is made up of Greek words poly-means more than one, morph means form. So polymorphism can be thought out as an
Act of existing in more than one form. 
 E.g we behave differently to same stimulus at different situation.Suppose you are moving in mall and see a beautiful lady giving you a nice smile you might return a smile, chat a bit with her, may go for coffee in normal situation, but if you are with your girlfriend I bet if you can mange a smile in return the way we react to stimulus is handled by brain at time you exposed to it (compile time polymorphism).
   Well in programmatic sense you can find average of two integers or two real numbers using same interface if you pass two integer corresponding implementation get called if you pass two real numbers corresponding implementation is called.

No comments:

Post a Comment