Four Pillars of Object Oriented Programming (OOP)

Four Pillars of Object Oriented Programming (OOP)

There are four Pillars of Object Oriented Programming:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Lets try to understand each of them in a most easiest way!

1. What is Abstraction?

Abstraction of Data or Hiding of Information is called Abstraction! or in other words, what are those things that a user is concerned about. Lets try to understand it by taking an example of an Uber app!

Image for post

Lets try to understand the flow of the app:

  1. In figure (A): When you want to book a ride, you have to Enter a pick-up point.
  2. In figure (B): After done with touching on Enter a pick-up point, now you have to enter your destination place (Where to?).
  3. In figure (C): After done with typing the destination place now you have to select a place from the suggestion list.
  4. In figure (D): After done with selecting one from the suggestion list, you found your estimation of time and cost.

What interesting about each point is you do all your actions with just one touch and you don?t need to worry about how its happening like when you touched on Enter a pickup point then how the map is opened and how that pointer points to your location? Did you need to know that? What you concern about at that point was to locate my location correctly, and that?s it! and that?s exactly what Uber App doing for you (i.e. information hiding).

2. What is Encapsulation?

Binding of Data and Functions (that manipulate the data) together and keep both safe from outside interference and misuse is called Encapsulation. Lets try to understand it by taking the following example!

Image for post

Lets try to understand it. Let?s say we have a class called Mobile and we have some public functions (public means the function can be called outside from the class) :

  • openCamera()
  • openClock()
  • openCalender()
  • openMaps()
  • openNotes()
  • openAppStore()

and so on. Above figure shows the comparison of mobile phone with a camera feature (middle one) and another mobile phone with a camera feature including a further feature of zoom in (right one). Let?s talk about openCamera() function. Initially you just have a function which opens camera for you! nothing else. Then you have got a new idea in a shape of (Zoom in) feature. To implement this idea you only have to update your OpenCamera() function. The point that needs to understand here is you don?t need to worry about anything outside of this function. You just go to the function, make some changes and BOOM!. That is what Encapsulation offers you (the mechanism of implementing class and function and making things together).

2. What is Inheritance?

Inheritance enables new objects to take on the properties of existing objects. There are different ways in which Inheritance can be done.

  • Single Inheritance
  • Multi-level Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance

Lets try to understand first type i.e. Single Inheritance by taking an example of Facebook Messenger App.

1. Single Inheritance

Image for post

In this type of inheritance, a child class inherits the behavior from parent class so in above example, child class is GroupCall class which inherits the behaviors (i.e. startAudioCall() and startVideoCall()) from parent class i.e. Call. The point that needs to understand here that instead of defining those method (i.e. startAudioCall() and startVideoCall()) again in GroupCall class, those methods will be inherited from the parent class. So in this way inheritance is done!

2. Multi-level Inheritance:

In this type of inheritance, a relation of Grand Parent to Parent to Child is followed. Let?s try to understand it by taking an example:

Image for post

In this type of inheritance, a child class inherits the behavior from parent class which further inherits from grand parent class. Here child is Primary Mail, parent is Gmail, and grand parent is Mail.

3. Multiple Inheritance: (not supported by Java)

In this type of inheritance, a relation of one child with two parents is followed. Let?s try to understand it by taking an example Instagram Stories:

Image for post

Here we have taken an example of Instagram stories, where a class Stories inherits some behavior functions of class Camera and some behavior function of class Filters. so here a child i.e. Stories inherits from one parent (Camera) and another parent (Filters).

(Multiple Inheritance can be done in java by using Interfaces).

4. Hierarchical Inheritance:

In this type of Inheritance, A relation of multiple children of one parent

Image for post

As you can see in the figure, we have taken an example of WhatsApp where a message can be send. So Message is a parent here and Text Message and Audio Message and Photo Message are children of Message!

4. What is Polymorphism?

It is the ability to redefine methods for derived classes. or we can say that object can behave in different forms is call Polymorphism. Lets try to understand it by taking an example of Google Search engine.

Image for post

Lets try to understand it. When user searches for something (ALLAH HU AKBAR) in this case. so by default SearchItems () of ALLSearch class will be called. Similarly when user clicks on Images to see the Image of the search query then SearchItems() of ImageSearch class will be called. The point that needs to understand here that redefining of function is done here and secondly object of Search class can be appeared in many forms (like object of AllSearch, ImageSearch etc) implementation of the above example in Java can be done as follow:

Image for post

Thats it! Happy Coding 🙂

No Responses

Write a response