DEV Community

Cover image for The PIE concept of OOP.
Enakshi Pal
Enakshi Pal

Posted on • Updated on

The PIE concept of OOP.

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.


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?

• 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!

Top comments (9)

_hs_ profile image
HS • Edited

Sadly to really start with OOP, the real idea behind it, one would have to ditch class-based programming and focus more on functional ones. Yes, functional ones as in Erlang because of it's actor-based style. You can use something like Scala with Akka to get all features in a single language but classes are deeply troublesome. Here's a link with the AK that coined the term OOP. And here's another one. So all in all JavaScript might be closer to OOP without using classes than Java or C#. Don't get me wrong I mainly focus on Java because I started with it and it has great ecosystem (as in many things are written so you don't have to :D) but in all fairness given the chance I would switch to Scala and try more "actor" based approach. The thing is that data structures that guarantee type safety are good for me personally and I do mutate stuff in certain ways but not simple data just flowing between HTTP and DB with some minor validations. I think having "modules" that contain functions or static methods or whatever you call it would be good and also allowing classes and some mutations. Scala and Kotlin have tried it but I don't really like how they did it. So, for now, I still end up using class keyword but I rather think of it as better encapsulated package or module... Whatever the keywords are Objects in OOP should be looked upon as microservices then we will see wrongdoings in class classes based approach.

enakshi_pal profile image
Enakshi Pal

Thanks for sharing the resources! :)
Also, your points are good about the class-based approach. I liked it. :))

matthiasuwe profile image


I like the article and it goes down to basic principals, which are really important for OOP.

But I'm not going along with your examples. Let me show you for Polymorphism:

"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?"

What you are describing, is a state change within the same object - one person is reacting differently but not because of Polymorphism but because of emotions (attribute) of the object.

Run-time Polymorphism means, you have different objects and calls them all the same way. But they are reacting differently (overwritten methods).

Let's keep the example of people - think about a situation, there is an medical emergency and 10 people are standing around. You are asking each of them "Please help this person!" - 9 of them are responding with "I will call an ambulance". But one will start to re-animate the person?
Why? This is about Polymorphism. You have the same, narrowed view (narrowing cast) to these 10 people, but they are not just "people", they might be layers, construction workers or something different. But one out of the 10 is a doctor who has the method "helpInMedicalEmergency" function overwritten. This person is able to help immediately, the rest of the people have "standard" implementation (from parent class) for the same function.

The important point - the moment, you are asking, you just know each of them is "people" but while asking (runtime) the actual object decides about the most specific implementation which will be executed.

What do you think?

Kind regards,

enakshi_pal profile image
Enakshi Pal

Hey, I liked the way you elaborated the example. Quite understandable! Thanks!

romaincarrillo profile image

Hi Matthias,
Thank you for this exemple, it makes it way more clear to me.

pconrad profile image
Phill Conrad

I wonder if you accidentally reversed the code excerpts for overloading and overriding? I could be wrong, but they appear to be backwards.

enakshi_pal profile image
Enakshi Pal • Edited

Hey, thank you so much for pointing it out! I have put them back to their appropriate places!

joyjit43 profile image

Really well explained! Wish to see more such blogs.. Get going!!

enakshi_pal profile image
Enakshi Pal

I am delighted you liked it!
Keep supporting. :)