Encapsulation & Abstraction
You might wonder why I have taken these two topics together. Well, we will see that thing later. First take simple definition from Google:
Encapsulate: enclose (something) in or as if in a capsule
Abstract: existing in thoughts or as an idea but not having physical or concrete existence
Now take a simple example of them. Let’s assume that you bought a new house. You want to transfer all your luggage from the old house to a new house. So you have started packing your luggage like:
- Put all clothes in one big box and labeled it “Clothes”.
- Again put all books in another box and labeled it “Books”.
So, in this example, these boxes are encapsulation and labels are an abstraction.
Let’s take another example of your stylish smartphone. It has metallic or fibre body, micro chip, front & rear camera, mic, speakers, battery, sensors like accelerometer, gyroscope, screen turn on/off button, volume up/down button and yes most important touchscreen. But you can only access screen turn on/off button, volume up/down button and touch screen. So these accessible things, which provide a way to interact with your smartphone, are an abstraction and phone (I mean body) which is encapsulation.
Now take a bit of a technical example of the Camera class of any OS. The main objective of the Camera class is to take a picture of any object. For that, it has code that interacts with camera h/w and such code is not accessible to the outside world. So, User of Camera class, I mean another developer, who hasn’t developed that Camera class, has no idea ‘How does camera class interact with hardware?’. All he wants is “How can he take a picture?”. For that Camera class has provided a simple method named
takePicture(), which is accessible to the outside world. By using this method, our ‘another developer’ can take a picture from the camera without having knowledge of Camera H/W interaction.
So, in this example, takePicture() method is abstraction and Camera class is encapsulation.
Now, you got it why I took these two topics together. 🙂
In conclusion, abstraction describes what to expose and what not to expose. And encapsulation describes how to expose and how to hide.
Polymorph: object or material which takes various forms (From Google)
Let’s take an example of me. I am a son of someone. I am a brother of someone. I am a friend of someone. I am a writer of this blog. So, I behave differently at a different place. I am polymorphism and so is everybody! Right??
Again take an example of your smartphone. You can talk with someone, you can enjoy your favourite music, you can play games, you can capture images, you can record audio and much more. So, your smartphone is a polymorphism.
Now take a technical example of ‘+’ sign (In Java). ‘+’ sign do the addition for Numbers and the concatenation for Strings. So, it is a polymorphism.
Inherit: receive(money, property, or title) as an heir at the death of the previous holder (Again from Google)
Let’s take an example of Game of Thrones. After the death of Robert Baratheon, his elder son Joffrey Baratheon became a King of seven kingdoms of Westeros. So, that’s inheritance.
Let’s take another example. Corded phone, from it, we can talk with someone. Mobile phone (like Nokia 3315), from it, we can talk, send a message, put alarm and other basic functionalities. Smartphone (like iPhone, Nexus, Galaxy devices), from it, we can talk and lots of other basic and advanced functionalities. So here, Corded phone is a grandfather, Mobile phone is a father and Smartphone is a child. so, that’s inheritance!
No need for technical example, right? 🙂
I hope you got a clear explanation of OOP concepts. Easy, isn’t it?
Please share your thoughts.