In this article, I am going to discuss OOP concepts with real-world examples. First of all, let's see what is OOP?
What is OOP?
OOP - Object-Oriented Programming is a programming paradigm that is based on objects. (Objects - real-world entities like book, vehicle, tree, etc..)
Now let's see what are OOP concepts.
There are 4 OOP concepts. They are,
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
Let's discuss it one by one.
1. Polymorphism
Polymorphism is the ability to exist in many forms.
Example 1: We can take a boy as a real-world example. This boy can be a student, a player, and a writer. So that this boy can exist in different ways in different situations.
Example 2: Sound of animals. People have the same sound but different animals make different sounds. The following diagram shows few different sounds make by animals.
This what we called as polymorphism.
2. Inheritance
Inheritance means it allows classes to inherit common properties from the parent class.
Example 1: let's assume that there is a class as Vehicle. All vehicles are not the same. We can inherit common properties like color, size, type from the parent vehicle class and create classes like Car, Bus, Bike.
Example 2: let's take another parent class as Animals. Here also we can inherit common properties like name, sound, color, breed from Animal class and create classes like Dog, Cat, Horse and etc.
3. Encapsulation
Encapsulation means it binds data and code together into one unit.
Example1; the most commonly used example is the medical capsule. This capsule mixes few types of medicines and stored in one capsule.
Example 2: another example for encapsulation is a large organization. An organization is consists of several departments like the production department, purchase department, sales department, and Accounts department. It combines all these departments together and had formed the organization.
This is what we called as encapsulation.
4. Abstraction
In abstraction, it displays only the important information by hiding the implementation part.
Example 1: Let's take the ATM machine. In an ATM machine, we can perform functions like withdraw cash, deposit cash, check balance, print bills, and so on. Even though it performs a lot of actions it doesn’t show us the process. It has hidden its process by showing only the main things like getting inputs and giving the output.
Example 2: The next example is the most commonly used mobile phones. On a mobile phone, we can perform so many actions like making a call, sending messages, take pictures, download software and etc. We perform a lot of things but here also we don't know the inside process of these things. Which means the implementation parts are hidden.
This is what we called as an abstraction
So that's about OOP concepts with real-world examples. I hope you got a clear idea about these concepts.
Thank you for reading my article.