The PIE concept of OOP.
Tags: codenewbie, womenintech, productivity, java
Published: 2021-06-19 09:18:55

Being a beginner in the technical field, knowing one of the object-oriented programming languages in detail, is a MUST! 

Let me add to that. Here we are NOT going to discuss about “Which language to choose, to ace DSA?” or “Shall we go for C++? Java or Python?” 

But here we shall discuss about the basics of development, i.e., __OOP__. More accurately we will address the __building blocks of OOP.__

Building Blocks of OOP?

Yes! You read it right. In any interview, whenever you claim to know about an Object-Oriented Programming (OOP) language, you are expected to have a clear concept of OOP.
 
Besides everything, one question that you are most likely to get hit by, is about, **“What are the important features of OOP?”**
 
Before you start digging deeper, let me explicitly mention that, here we would take Java as a reference to understand the OOP concept. So, lets dive in! 

Let me get you familiar with certain terms before we actually start.

**Parent Class**: _The class whose properties and features are used or inherited by another class is known as Parent Class._

**Child Class**: _The class which extends or use the functionalities or properties of the parent class is known as Child Class._
 
#Have you heard about the term PIE?

No. It’s definitely not the Apple Pie we eat but, this is something related to OOP. Curious, yet again. Aren’t you?

Let me break the ice. The **PIE** here is referred to the basic building blocks of OOP. Let’s have a look!

1. P - __P__olymorphism
2. I - __I__nheritance
3. E - __E__ncapsulation

Now let us break the PIE into bite-size pieces! 

In PIE, the **‘P’** stands for **Polymorphism.**
 
According to the dictionary, _Polymorphism is the condition of occurring in several different forms._ If we split the term, we see that Poly means _“many”_ and Morphism means _“forms or shapes”._ Polymorphism is a concept in OOP by which we can perform a single action in many forms.

The best example to understand Polymorphism would be _Human Behavior._ 

##How?! 

A person can be happy at this moment and at the next moment he can be sad or angry. Here, a single person is functioning **multiple emotions.** This is how Polymorphism works! Relatable enough?

Technically, Polymorphism allow us to define one interface with multiple implementations.

Let us look into the types of Polymorphism. It has 2 types.
1. Compile-time Polymorphism
2. Run-time Polymorphism

Let’s have a sneak peek!

**Compile-time Polymorphism:** In Java, this type of polymorphism is achieved by **_Method Overloading._** When there are multiple functions with same name and different parameters then these functions are said to be overloaded.

This is also known as Compile-time Polymorphism because, the decision of which method is to be called is made during compile time by the compiler. This is also known as _Static Polymorphism or Early Binding_ because the compiler resolves the method calls based on the argument list and **_reference_** on which we are calling the method. 

Below is a code snippet for overloading
 
![Code Snippet for Overloading](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0z6t4o0g46ge8jnbc5x.png)

**Run-time Polymorphism:** This is achieved by performing **_Method-Overriding._** When a method in the child class having same method signature and return type as its parent class, provides specific implementation of a method that is already provided by its parent class, then the method is said to be overridden.

This is also known as _Dynamic Polymorphism or Late binding_ because here the overridden method call gets resolved during **runtime.** It is the type of the object being referred to that determines, which version of the overridden method will be executed.
 
Below is a code snippet for Overriding.

![Code Snippet for Overriding](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyuhxazl7mfqcuclee2f.png)

Let us now look at the _‘I’ of PIE_!

The **‘I’ of PIE** stands for Inheritance. I assume that you are a person of beauty (inner and/or outer) with brain so, where have you got these qualities from? 

Of course, you must have inherited these qualities from your parents! This is exactly how the concept of Inheritance works. 
This concept allows the child class to inherit the features of its parent class.  

##Why do we need Inheritance?

Because…
• It decreases code redundancy.
• It supports the concept of code reusability.

Let me elaborate this with an example.

_We use reusable or refillable pens for writing on paper so that once the ink gets over, we do not need to change or buy a whole new pen, we need to buy a new refill instead. Here we are using the same old pen with new refill. Here the old pen is the **Parent Class** and the new refill is the **Child Class** with it’s new features._ 

This is exactly how the concept of Inheritance works. Sounds interesting, doesn't it?

Let me put a question. Are you aware of the term **‘Multiple Inheritance’** ? Yes? No? or None of them, may be.

So, _Multiple Inheritance is that concept of OOP where a child class inherits the properties of more than one parent class._
  
_**Fact** : Multiple Inheritance is not supported by Java because, the compiler becomes ambiguous regarding which parent class method to call._

Below is the code snippet to understand Inheritance.
![Code Snippet for Inheritance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ooze94mna70vz8x8aiap.png)

Last but not the least, let us have the last bite of the PIE, i.e., the last letter ‘E’, which stands for **_Encapsulation_.**

Basically, the term encapsulation itself defines _the action of enclosing something, as if it is a capsule, i.e., it is the wrapping of data in a single unit._
 
**Encapsulation = Data Hiding + Data Abstraction.**
 
Now, what’s that? 

Wait! Let’s split the equation first and then have a sneak peek!

According to the equation, we see that **Data Hiding** is the process which hides the internal data from you (the user) whereas, **Data Abstraction** is the process where certain internal implementation of particular set of services are hidden from you (the user) and only the essential information is shared with the user.

Let us look at encapsulation with the help of an example.

_When you login to your email account, from the moment you open it till, you put your username and password, a lot of internal processes take place in the backend and you have no control over it. These internal processes are being hidden from you through encapsulation, so that you do not have to worry about the working procedure of that particular application!_
 
Therefore, the main purpose of encapsulation is to make sure that the delicate set of data is hidden from you (the user).

Thus, learning OOP would help you in many ways, such as,
    
    • Faster Development.
    • Reuse of Previous work.
    • Helps in better mapping to the Problem Domain.
    • Also plays an important role in Client/Server Applications.

Now that you know about the essentials of OOP, getting along with the development environment would become easier.
 
Till then keep exploring and keep learning!

_Happy Coding!_