In many .NET and JAVA interview, one of the basic, yet important question being asked is, "What is abstraction" OR what you understand by Abstraction in OOPS?
Abstraction is one of the building block of Object Oriented Programming & Systems (OOPS). Let's go through the below topics to understand abstraction in OOPS.
- Abstraction English dictionary meaning
- Real world abstraction example
- Abstraction in OOPS definition
- Advantages of abstraction in OOPS
Abstraction English dictionary meaning:
- An idea not associated with any specific instance.
- The process of formulating general concepts by abstracting common properties of instances.
Real world abstraction example
By looking at the below picture what comes to your mind?Possible guesses may be one of the below:
- Picture shows different kinds of cars.
- Picture shows different manufacture's car.
What made you think, "these are different kinds of cars?"Your reasons may be one of the below:
- All the different images in the picture have 4 wheels.
- All have a steering wheel.
- All have an engine and a body.
If you look at the above points, it is clear all the above are characteristics of the car. Without any of the above features, it may not be possible to call a object as Car. If a object doesn't exhibits any of the above features, we will not call that object as a Car. That means if we want to call an object as Car, it has to have all the above features, without which its not a car.
By now you are clear what is all about abstraction. Define a Car abstract class or an interface(Icar) in your programming language and all other different cars should provide implementation for the ICar interface. Look out for common or general features of an object and define those features in a abstract class. Then all different types of the the object have to provide the implementation for that abstract class, without which the object is not called an instance of the abstraction.
Abstraction in OOPS definition
Abstraction is a building block in OOPS. Allows loose coupling and defines common features for related objects so that they can be interchangeable.
Advantage of using abstraction
Using abstraction in OOPS design bring loose coupling in the system. When system is loosely coupled with dependencies, you can easily plug & play the dependencies. Hence less of regression while switching to another dependency.