02 February 2013

What is abstraction in OOPS?

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.

  1. Abstraction English dictionary meaning
  2. Real world abstraction example
  3. Abstraction in OOPS definition
  4. Advantages of abstraction in OOPS

Abstraction English dictionary meaning:

  1. An idea not associated with any specific instance.
  2. The process of formulating general concepts by abstracting common properties of instances.

Real world abstraction example

Abstraction explained with real world example

By looking at the below picture what comes to your mind?

Possible guesses may be one of the below:
  1. Picture shows different kinds of cars.
  2. Picture shows different manufacture's car.

What made you think, "these are different kinds of cars?"

Your reasons may be one of the below:
  1. All the different images in the picture have 4 wheels.
  2. All have a steering wheel.
  3. 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.