In this section we will dive deep into Object-Oriented paradigm and understand its 4 major concepts namely:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
If I must describe each of these concepts in just one sentence, then:
- For Abstraction, I will say Eliminate the irrelevant & amplify the essential
- Encapsulation means Hiding the unnecessary
- Inheritance helps in Modelling the similarities
- Polymorphism means Same function different behavior
Abstraction refers to the act of representing essential features without including the background details or explanations. The core things here are - essential features and not including background details. Lets first try to understand the essential features part.
Real-World Example of Abstraction
Consider this example where a lady has brought her pet cat to a vet for treatment. Let's see the cat from Vet's point of view, what are the essential features of the cat for the Vet?
Vet’s aim is to examine the cat and diagnose any health issues the cat is having so for the Vet the essential thing is the body structure of the cat. The Vet is interested in the different body parts of the cat like heart, lungs, stomach, kidney, intestine, liver bladder to ensure everything is functioning perfectly and the cat is healthy. Some features irrelevant to the Vet are the favourite toy of the cat, what food the cat likes the most or what is the color of the cat's bed. These things are of little help to diagnose any health problems the cat is having so they are mostly irrelevant to the Vet.
Now let's see the cat from the lady's point of view
Things about the cat that matter to the lady are cat's favorite toy, favorite food, color of cat's bed. Because cat is the lady’s pet, everything that makes the cat feel happy is essential to the lady. On the other hand, things like the anatomy of the cat, scientific names of cat's body parts, etc are irrelevant to the lady as these things doesn’t help the lady to make her cat feel happy.
So now I hope you have a better sense of how essential information depends upon the problem at hand. Depending on the problem domain, we need to eliminate the irrelevant & amplify the essential.
There were two core parts in the definition of Data Abstraction. We have already looked at and understood the essential features part. Now, let's try to understand hiding the background details part with the example of a switchboard.
How do you switch on a light or fan in your room at home? Obviously, you go to the switchboard, press the required switch to turn ON the light or fan. You don't worry about how that switch is turning the light ON, what is the internal wiring, etc. Switchboard is hiding all such background details from you and providing you a very simple way to switch on or off the light.
You encounter such examples of Abstraction every day in your life. While driving a car the driver presses accelerator to speed up the car and brakes to stop it. He doesn’t worry about all the background details regarding how the engine is working, how the pistons in the engine are firing up at an increased RPM to speed up the car or how the brakes are working to slow down and stop the car. All these details are hidden away from the driver and he is presented with two very simple interfaces – accelerator and brakes to speed up and slow down the car. Such hiding of background details through Abstraction is making our day to day lives simpler.
Java supports Abstraction through abstract classes & interfaces. We will look into this a little bit later in this course.