OOP in real life?
Most of the time you might have been asked this question in the interviews. And I believe it is always easy to understand a concept with a practical example. In this article, I will explain how the OOP concepts work in the real world. First of all, let's get to know what is OOP.
What is OOP — Object Oriented Programming?
“ Object-oriented programming is a programming paradigm based on the concept of ‘objects’. “
In the real world, these objects can be vehicles, people, animals, etc. When it comes to programming these objects can contain data and code: data in the form of fields and code in the form of procedures.
There are 4 OOP concepts.
- Abstraction
- Inheritance
- Polymorphism
- Encapsulation
Let s discuss each of them separately.
Abstraction
Simply abstraction means hiding the implementation part and displaying the important details.
Think about a coffee machine. You just need to know how to use the machine and have to provide the ingredients. You don't need to know the mechanism behind making the coffee.
Benefits:
The abstraction class hides the implementation part, reduces the code's complexity, and increases efficiency.
Inheritance
Inheritance allows the sub-class to get the common properties from its parent class.
Take animal as a parent class. It has properties like animal type,color,sound, etc.That parent class can be extended by cat , dog, lion sub classes. So those sub classes can inherit the properties of the parent class.
Benefits:
- The most important use of inheritance in Java is code reusability. The code that is present in the parent class can be directly used by the child class.
- Method overriding is also known as runtime polymorphism. Hence, we can achieve Polymorphism in Java with the help of inheritance.
Polymorphism
Ability to exist in several forms.
A boy can have different characters. Like he can be a student, writer, and also a player. So that person can have different characters in different situations.
Benefits:
- Programmers' code can be reused via Polymorphism.
- Supports a single variable name for multiple data types.
- Reduces coupling between different functionalities.
Encapsulation
This simply means data binding. The bundling of data, along with the methods that operate on that data, into a single unit.
We can take a capsule as an example and it shows in its name also. Capsule binds severa vitamins into one unit.
Benefits :
A class can have complete control over its data members and data methods. The class will maintain its data members and methods as read-only (Using getters and setters ). Data hiding prevents the user from the complex implementations in the code.
So using these simple examples you can take a basic knowledge of oop concepts.
Cheers !!