Pillars Of OOP/Overview Of OOP

Object Oriented Programming

A programming model which is mainly organized around the objects is called Object Oriented Programming or the Programming where the main focus of programmer while modeling any problem is objects. (The name OOP indicates the same).

What is Class ?

Class is a template/blueprint for the Objects. It defines the object behavior (in the form of class methods) and object attributes (in the form of properties or class data members).

A software system may consist of many classes (A real time system may have thousand or more classes). Working with so many classes is not so easy, so we require some way, so that they can be managed properly or efficiently.

In order to manage so many classes and to reduce the complexity, system designers use several techniques which can be grouped under four main concepts Encapsulation, Abstraction, Inheritance and Polymorphism.

These concepts are the four main gods of OOP world and in software term, they are called four main Object Oriented Programming (OOP) Concepts (or four fundamentals of OOP or four pillars of OOP).

In the following paragraphs for each pillar, I have given real life example. The advantage, in most of the interviews I faced that question (what is real life example of abstraction) and it will make the picture of corresponding very clear.

1. What Is Encapsulation

Encapsulation is a process of binding data members (variables, properties) and member functions (methods) together. In object oriented programming language we achieve encapsulation through Class.

Real Life Example Of Encapsulation:

The real life example of encapsulation will be the Capsule. Capsule binds all chemical contents required for curing specific disease together just like the class which binds data members and member functions.

Benefits/Advantages Of Using Encapsulation:

  • Encapsulation As Information Hiding Mechanism:

    As already discussed, encapsulation is a process of binding data members (variables, properties) and member functions (methods) together. Due to this approach the contents (or data members) of object is generally hidden from the outside world (By simply making the data members as private) which is nothing but information hiding.

    Typically, only the objects own methods can directly inspect or manipulate its fields. So the outside world only knows what a class does (Through the member functions) but don't know what are its data members and how that class do a specific task.

    Hiding the internals of the object or allowing access to it through only member function protects objects integrity by preventing users from setting the internal data or the component in such a way that may lead to an invalid or inconsistent state of object.

    Implementation In Programming Language:

    1. Public Class Account  
    2. {  
    3.        private double balance;  
    4.        public double GetBalance()  
    5.        {  
    6.          return balance;  
    7.        }  
    8. }  
    9. //Imagine main as outside world  
    10. public static void Main()  
    11. {  
    12.         Account obj=new Account();  
    13.         double currentBalance=obj.GetBalance();  
    15.         //Following thing is not possible  since that data member is private   
    16.         obj.balance=20000000000;  
    17. }  
    Imagine what disaster could have been happened if the balance (data member) is made public. We could have been the richest person of the world.

2. What Is Abstraction

Abstraction is the process of showing only essential/necessary features of an entity/object to the outside world and hide the other irrelevant information.

In programming language we achieve the abstraction through public and private access modifiers and a class. So in a class make things (feature) which we want to show as public and thing which are irrelevant make them as private so they won't be available to the outside world.

Real Life Example Of Abstraction:

Real life example of Abstraction could be the gears of bike. Do we know what happens inside the engine when we change the gear ? Answer is No (what happens inside the engine when we change the gear is irrelevant information from user perspective so we can hide that information). What important from users perspective is whether gear has been changed or not. (This is essential/necessary feature that must be shown to the user).

So the abstraction says only expose that details which really matters from users perspective and hide the other details.

Benefits/Advantages Of Using Abstraction:

Advantage of Abstraction is, it reduces the complexity of end users since to the end user we have shown only things that are necessary to him and not shown the unnecessary things. Imagine what would have happened when we told the bike riders things happening inside the engine (i.e. irrelevant information) when he change the gear. So it would have increased the complexity of bike riders.

Implementation In Programming Language

Imagine a car. Now let's think in terms of car rider or a person who is riding a car. So to drive a car what a car rider should know and what not.

Necessary things:


Unnecessary things:

    Exhaust System
    What happen when he change the Gear of car.

Here I have written the code snippet in C# for explaining the above mentioned things:

  1. class Car  
  2. {  
  3.      public string Brakes {get;set}  
  4.      //Gear is exposed using the public access modifier.  
  5.      public string Gear{ get;set;}  
  6.      public string Steering {get;set;}  
  9.      private void Exhaust_System()  
  10.      {  
  11.        ………..  
  12.      }  
  13.      //Hided using the Private Access modifier  
  14.      private void What_happen_we_he_change _the_Gear_of_car()  
  15.      {  
  16.        ………..  
  17.      }  
  18.      private void Silencer()  
  19.      {  
  20.         ……...  
  21.      }  
  22. }  
Note: Abstraction and abstract class are two different things so they are not much related. Do not confuse by comparing those two. Consider them as independent.

3. What Is Inheritance

The process of creating the new class by extending the the existing class is called inheritance or the process of inheriting the features of base class is called as inheritance.

The existing class is called the base class and new class which is created from it is called the derived class.

Real Life Example Of Inheritance:

We inherit some of our features (may be body color, nose shape, height of body etc) from Mom and Dad.

Benefits/Advantages Of Using Inheritance:

The most important advantage of inheritance is code re-usability. In Inheritance the derived class possess all attributes/properties and functions of base class, this is where code re-usability comes into the picture. So no need to write same function and attributes of base class in derived class.

Note: Private members of base class also get inherited in derived class but they are not accessible in derived class.

Implementation Of Inheritance In Programming Language:
  1. //suppose initially I wrote a base class shape as   
  2. class Shape  
  3. {  
  4.      public int width;  
  5.      public int height;  
  6.      public void SetWidth(int w)  
  7.      {  
  8.         width=w;  
  9.      }  
  10.      public void SetHeight(int h)  
  11.      {  
  12.         height=h;  
  13.      }  
  14. }  
  17. //derived class rectangle  
  18. class Rectangle:Shape  
  19. {  
  20.        public int GetArea()  
  21.        {  
  22.  return width*height;  
  23. }   
  25. }  
Like rectangle we may have parallelogram, rhombus so there is no need to declare width and height each time, just inherit those classes (parallelogram, rhombus) from Shape class. This is how code reusability come into picture.
  1. class Tester  
  2. {  
  3.       static void Main()  
  4.       {  
  5.           //Derived class object  
  6.           Rectangle obj=new Rectangle();  
  7.           obj.SetWidth(5);  
  8.           obj.SetHeight(10);  
  9.           Console.WriteLine(“Area of rectangle is {}”,obj.GetArea());  
  10.           Console.Read()  
  11.       }  
  12. }  
What Is Polymorphism

Poly means many and Morph means forms. Polymorphism is the process in which an object or function take different forms.

Real Life Example Of Polymorphism:

Real life example of Polymorphism is mobile phone. It is a single object but it can be used for making calls, listening music, sending mails, taking pictures, etc (different forms).

Implementation Of Polymorphism In Programming Language:

We can implement polymorphism using method overloading, method overriding, etc.
  1. //Implementation of polymorphism using method overloading  
  2. class Base  
  3. {  
  4.       public int Add(int num1,int num2)  
  5.       {  
  6.               return(num1+num2);  
  7.        }  
  8.       public int Add(double num1,double num2)  
  9.       {  
  10.               return(num1+num2);  
  11.       }   
  12. }  
  13. class Program  
  14. {  
  15.       public static void Main()  
  16.       {  
  17.             Base obj=new Base();  
  18.             Console.WriteLine(“Addition of two integer number is ”+obj.Add(4,5));  
  19.             Console.WriteLine(“Addition of two float number is ”+obj.Add(6.78,5.23));  
  21.        }  
  22. }  
So what we observe is function Add takes two form. First form is for adding integer numbers and second form is for adding float numbers.

Advantages/Benefits Of Using Polymorphism:

The main advantage of polymorphism is, it makes the life of end user easy since instead of having two methods (as shown in above example) AddInt for adding integer and AddFloat for adding the float we have only one method Add. So end user have to remember only Add method and not AddInt and AddFloat.