Monday, October 18, 2010

OOPS four pillars

In this post we will see four pillars of object oriented programming (OOPS).
Starting from the beginning we had C programming which is procedural oriented or you can say more function oriented, you can make robust application in C, a very good example of this would be your windows operating system which is made in C. So now the question rises that when such brilliant applications can be made by using a procedural oriented language (POP) then why do we need OOP.
The answer that why do we need OOP is given below:
  1. In POP large programs are divided into functions, while in OOP they are divided into class whose object can be used later.
  2. POP has no access specifier (private,public,protected) while OOP has access specifier which help the programmer to have a grip on the variables which carry important data.
  3. In POP most of the function share global data now the disadvantage here is that a function may also try to update the value of the global variable and if any of the function does update the global value then in that case you are not sure about the accuracy of the result that your program will give you.
  4. In POP operator cannot be overloaded.

Before beginning with OOPS i would like to have a small discussion on classes and objects

Classes and objects: 
classes are the blueprint of your actual object, consider a blueprint of a building the engineer looks at the blueprint and based upon that blueprint he builds a building, the final product in this case is building which is said to be the object in programming terms, based upon one blueprint the engineer can build 10 or 20 building but the name of those buildings are different which means that we cannot give the same name to the object of our class we have to give it different names else it might create problem. Objects have states and behaviors, states means property and behavior means methods or functions.

Now coming to the four pillars of object oriented programming: 

Abstraction: It means to hide the unwanted information and giving only the relevant information.
Example: say you want to buy a car and you go to a car showroom asking the salesman to show you a business type car, the salesman shows you audi. You ask him about its price and engine type and the relevant information is provided by that salesman. This is known as abstraction means giving only relevant data to the user and not the complete data. 

Encapsulation: It means to hide the complete information from the user.
Example: As a developer you build the application and give the end product to the user /client, you give him the setup file that's it, you don't provide the user the source code, your code is encapsulated in that setup file from which the user is unaware that what code or what language you have used to code for this particular application he just wants the end product that's all. Another example would be the medical capsules.  For us it is just one particular capsule but if  remove its upper cap we can see that their are mixtures of more than one medicine in that one capsule so here too the patient is unaware of the mixtures and thinks that its just one particular capsule. 

Inheritance: This term means deriving some of the characteristic from the parent class.
Example: you are children of your parents in this case your parents are the parent class and you are the child class, and of course you must have derived some of the characteristic from your parents like your looks, hairs, attitude etc. you must also have come across people who must have said that you look like your mom or like your dad this means that you have derived looks from either your mom or dad. Besides the parent class characteristic the child class also has some of its own characteristic and behaviours. 

Polymorphism: If you divide the term "polymorphism" then you are left with two words that's poly and morphism. Poly means many and morphism means forms. It is often expressed by the phrase "one interface multiple function"
Example: Graphical user interface(GUI) where you have only one interface but at a time their are multiple functions which are working together to get your job done.

You can apply polymorphism to reduce the complexity within the functions. Polymorphism are of two types it can be either static or dynamic. In static the response to a function is decided at compile time while in dynamic it is decided at run time.

Example of static polymorphism: players of a team are decided before the match, that's something the audience already know before the match.

Example of dynamic polymorphism: In a soccer match at substitution which player will be entering the field and which player will be replaced that is know to the audience only during the play.

I hope you enjoyed this post of mine regarding OOPS concept. Happy iCoding


  1. Your examples and explanation definition is perfect, easy language...
    Thanks Ravi !

  2. seriously Ravi the way you have explain the OOPs is very nice even the Example which you have use that makes it easy

    Thank you!!!

  3. Very helpful and explained in simple words. thank you..