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!
- P - Polymorphism
- I - Inheritance
- 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.
- Compile-time Polymorphism
- 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
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.
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.
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!
Top comments (9)
classkeyword 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.
Thanks for sharing the resources! :)
Also, your points are good about the class-based approach. I liked it. :))
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?
Hey, I liked the way you elaborated the example. Quite understandable! Thanks!
Thank you for this exemple, it makes it way more clear to me.
I wonder if you accidentally reversed the code excerpts for overloading and overriding? I could be wrong, but they appear to be backwards.
Hey, thank you so much for pointing it out! I have put them back to their appropriate places!
Really well explained! Wish to see more such blogs.. Get going!!
I am delighted you liked it!
Keep supporting. :)