You are on page 1of 2

Before talking about the 4 pillars of OOP, let s first learn:

Why to use OOP? And why it came into existence? What was the drawbacks of proced
ural programming paradigm?
Let s take a Real world example and see how the problem is solved using both the c
oncept?
Imagine you are working in a Vehicle manufacturing company where you need to kee
p track of inventory system (i.e. parts). The problem is the company manufacture
parts for both Car and Trucks.
For cars, you will need to have information about:
Color
Engine Power
No of gears
Number of doors
Similarly for trucks you will need same information with some modification as:
Color
Engine Power
No. of gears
Capacity of Truck
How to proceed with the design in procedural programming:
In procedural programming, you need to design different programs for both car as
well as trucks. (Even a slight modification in the requirement forces you to de
sign it again completely from starting)
This is where procedural programming suffers a drawback.
Now with Object Oriented Programming (OOP)
The problem is much easier than it seems. All you need to do is find out the req
uirement which you found common for both; wrap it up in the base class. In the a
bove problem we can wrap function (Method) for Color, Engine Power, No. of gears
inside the main class i.e. Vehicle class. Next we will create two more classes
i.e. one for Car and other for Truck both of them will inherit the feature of ma
in class i.e. Vehicle class. Now for the method which is required only for Car i
.e. Number of doors, we can add it into the Car class separately and for truck w
e need add the method Capacity of truck into the Truck class all the other metho
d are inherited from the Vehicle class (i.e. base class or parent class or main
class).
Note: both Bus and Truck class inherit the base class i.e. Vehicle class so they
can access all the method specified inside the Vehicle class.
What actually we did in OOP programming is that instead of creating different pr
ogram to achieve slightly different requirement we divide the whole program into
classes and with the feature of Inheritance in OOP we inherit the functionality
of one class into other.
The best thing about OOP is that; if say there comes a requirement in future and
we need to make some changes in the program, let s say we need to add one more pr
oduct i.e. Bus into the existing system. All you have to do is just create a cla
ss Bus and inherit the property of Vehicle class into it and add the extra metho
d required only for the Bus class.
Phew, I hope that you now know the importance of OOP and why OOP is preferred ov
er procedural programming.
Now let s talk about the 4 Pillars of OOP in detail

1) Abstraction
Definition: Abstraction is the process of hiding out the working style of an ob
ject and showing only the required information of the object in understandable m
anner.
What does it actually mean?
Let s discuss it with the help of an example.
Smartphones. 99% of you might be having a smartphone with you and the rest 1% at
least know what a smartphone is. You use your phone to call, text, play game,
surf on the internet, even now you may be using your smartphone to read this art
icle. So my point is do you actually know how it s happening and which method and
which function your phone is calling to carry all the task for you. You don t and
I am quite sure about that. This is what we call Abstraction, hiding the complex
ity and only allowing you to view the information of the object in understandabl
e manner.
Let s have another example of Car. You know that you need to put on the keys, chan
ge the gear and accelerate in order to drive a car but do you actually know what
happens when you change the gear and accelerate, or say how the car engine work
s? You don t, and that s simple because you don t need to know all that complexity. Yo
ur Job is to just drive the car. That s what we call Abstraction.
2) Encapsulation
Definition: Encapsulation means wrapping up data and member function (Method) t
ogether into a single unit i.e. class. Encapsulation automatically achieve the c
oncept of data hiding providing security to data by making the variable as priva
te and expose the property to access the private data which would be public.
Let s discuss Encapsulation with a example
Let s take a simple example of BAG, yes your school and college Bag in which you k
eep all your stuff like books and pens and document etc. to keep it safe from th
e outside world. Here you are hiding your books and pen by putting them inside t
he Bag similarly in OOP we encapsulate the data and methods inside the class to
keep it safe and accessible only to authorized member.
3) Polymorphism
Poly=many morph= form
By name we can come to a conclusion that it s about something with many forms. In
OOP it is an ability of an object to take many forms.
Let s learn with the help of an example:
My name is Ankit: I am a brother, a friend, a son, a student depending upon the
situation i.e. parameter surrounding me. Similarly in OOP we can have a function
with different form but with same name. Do note that the name is always same. T
he factor which distinguishes one method from the other is parameters.
Let s say a method to calculate square() of a number, now the number can be of int
eger type, float type, or double.Based on the parameter type passed jre automati
cally calls the suitable method . This concept of OOP is known as Method Overloa
ding.

You might also like