The PIE concept of OOP.

Enakshi Pal - Jun 19 '21 - - Dev Community

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 - Polymorphism
  2. I - Inheritance
  3. E - Encapsulation

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

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

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

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.
Enter fullscreen mode Exit fullscreen mode

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!

. . . . . . . . . . . . . . .