Tuesday, 16 August 2016

ABSTRACTION

ABSTRACTION

This is the first post regarding the OOPS concept.  There are many oops concepts but I will be covering only the 4 (Abstraction, Encapsulation, Inheritance, Polymorphism). I will cover the concept of Abstraction in this post and will cover the others in later posts. Also will have post one more blog describing how these concepts work together and what are the best practices to follow.
So let's start with the Abstraction.

DEFINITION

First let's see the common definition of Abstraction "Abstraction is hiding the internal details and showing the functionality".
The definition is right but it is how it is perceived.  It is believed that Abstraction hides the internal details of a method. Like only using the method and not need to know the how the code is written. And one of the common example for this is a Car. We only use the Car and we do not need to know how the Car engine works , how the brakes work, how the AC in the car works.

It is all fine but then what does the Encapsulation means?
Now  will not discuss the difference between  Encapsulation and Abstraction in this post (Will cover this in the next one) so let's see an another definition. How about this definition

"Abstraction is the concept of showing the relevant information about the object in use".

That is only showing the information required and hiding all the other information about the object.
Let's go through the car example again, now for a driver it is a 4-wheel vehicle but for a bird it place to sit on, it can be a shelter for the driver when it is raining. The point is the same car is used for different purpose depending upon the situation and not knowing the other properties and behavior of the car.

Let's see with this with java code.
There is a Car interface with an abstract method drive.

public interface Car {
  void drive();
}

There is a Box interface with two abstract methods size and wieght
public interface Box {
  int size();
  int wieght();
 }

Now here is a class which implements both these Interfaces
public class Civic implements Car, Box {

  @Override
  public void drive() {
  //Implementations
  }

  @Override
  public int size() {
  //Implementations
  }

  @Override
  public int wieght() {
  //Implementations
  }
}

Now if we create object of this concrete class like below

Car car = new Civic();

we can only access the method of the Car interface even the Object is of the class Civic which contains two other methods.

car.drive();  only this method is accessible.

And when we create Object with reference of Box we will get access to the method of Box interface only.

Box box = new Civic();
box.size();
box.wieght();

The point here is when we take the reference of the Car then we only use the object for car purpose and we hide the fact that the Object(Civic) is also a box and vice-versa.

Not used proper Interfaces and Class in the Example but I guess you get the idea what the Abstraction concept is.

So Abstraction is showing the relevant information about the object in use and hiding the other properties and behavior.


 HOW TO USE

It is common thinking that Abstraction can be achieved either by Interface or by Abstract class.
But is these are the only two way we can achieve Abstraction.  If you understand the previous example then it is clear that even with the concrete class we can do abstraction.
for example :
if we create a concrete class which extends our Civic class like :

public class Civic100 extends Civic {

  public int modelNumber(){
    return 0;
  }

}

This class extends the Civic class and has its own method modelNumber(). And if I create an Object of Civic100 like below

Civic civic = new Civic100();

now I can only access the methods of Civic class even the object is of Civic100.
So we can use the Abstraction concept through the Inheritance concepts.
The Abstraction provide loose coupling so it should be in code where loose coupling is required.


BENIFITS
·         Felxibilty
·         Reusebility