Abstraction and Encapsulation Demystified

Abstraction in OOPS is well articulated, and states clearly of hiding the unnecessary details from other Components. Abstraction and Encapsulation comes hand in hand and often lead to misconceptions. In this article I would love to clear out all the ambiguity you ever had in mind about Abstraction and Encapsulation and their implementation in Java.

Wikipedia Definitions:

“In computer science, abstraction is the process of separating ideas from specific instances of those ideas at work. Computational structures are defined by their meanings (semantics), while hiding away the details of how they work.”

Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.”

In layman terms, abstraction means hiding, and encapsulation means packing / gluing things together.

Now since we have already read about “Abstraction” and “Encapsulation”, let us get in to the details and understand about them in depth. We will be looking at Encapsulation first and then we will be looking at the Abstraction. I will be providing some real world examples in order to make them simple to understand.

As stated above Encapsulation means packing / gluing stuff together. In Object Oriented Programming we talk a ton about the objects and classes, and we know that the objects can have state and behavior. This sort of packing of state and behavior is called Encapsulation. So every class we write in java is encapsulating the state (of what the created object is) and its behavior (of what it can do).

For example Person is encapsulated as given below

State Behavior

2 Hands

eats

2 Legs

walks

1 Head

thinks

etc.,

Person defined as a Class encapsulating the state and Behavior below

Java Code:

public class Person {
  private int legs = 2;
  private int hands = 2;
  private final int head = 1;

  public void eats(){
	...
  }
  public void walks(){
	...
  }
  public void think(){
	...
  }
}

In the above code, we have encapsulated the state and behavior of the person, by doing this me made sure that when you tell an Object is a Person then you will know that it will have “hands”, “legs” and “head” properties and will be able to “eat”, “walk” and “think”.

Imagine the same situation in Functional programming where you defined the state separately and behavior separately, you will need someone/something to tell where to look for a Person behavior and state. Hence, encapsulation makes it easier to understand and read the code by humans.

Packaging and Distributing in jar’s can also be thought of encapsulation, as we generally combine certain functionality, and then distribute to others. 

Coming to “Abstraction” its simply hiding the complicated low level details from other systems. Abstraction doesn’t really mean that it should not be visible at all, when we do abstraction we are actually reducing the complexity and increasing the understandability.

Abstraction will have the below goals

  • Simplify
  • Understandable
  • Usable / Programmable

Before going any further I would love to give some examples of Abstraction and then we will see how we achieve the same in our Java programming. In our day to day work we come across several example of Abstraction.

2013-tesla-model-s-front-1You have a very nice car, and you know a lot about driving your car and running it run so smoothly. You know that if you put the key and turn it the car will start, and if you put pressure on the accelerator it will go fast, and shifting the gears will make it run smooth and applying the breaks will stop the car.

muscle-car-engine-showdown-8

Now, did you observe that there are so many complicated stuff inside your car that enables you to perform all these on your car. This complicated stuff will combust the fuel in your engine, and generates the torque for it to move and uses the air pressure to stop when breaking. 

Auto RepairWhat if you need to put fuel, and then do some other stuff for just starting the engine, and do several other tasks for each of the functionality you wanted to do on your car. Will you be interesting and enjoying your ride on the car. “Definitely not, you expect these complexities to be abstracted from you”.

In the same way, we need to abstract the complexities of our code and project a simple and easy to use interface for our customers / consumers of our product. So that they can feel it easy to use, maintain and adopt to your programming.

Abstraction can be done in several layers, it depends on who is your consumer and how do you want to expose the system.

  • TCP protocol gives several layer of abstraction.
  • JPA pattern abstracts the SQL from the business logic.
  • MVC pattern abstracts the business logic to the frontend. 

And there are many more ways we do abstraction in programming and day to day life.

Now let us see some code that shows the abstraction.

Poorly Abstracted Properly Abstracted
/** Poorly abstracted class */
public class Car {
   
private Engine engine;
   
   
/** Call combustfuel, useBattery, sparkEngine to start the Car */
   
public void combustfuel(){
       

   
}
   
public void useBattery(){
       

   
}
   
public void sparkEngine(){
       

   
}
   
/** Call burnFuel, increaseTorque for Acceleration */
   
public void burnFuel(){
       

   
}
   
public void increaseTorque(){
       

   
}
   
/** Call stopFuel, useAirLocking, increaseTireTraction for Braking */
   
public void stopFuel(){
       

   
}
   
public void useAirLocking(){
       

   
}
   
public void increaseTireTraction(){
       

   
}
}
 /** Poorly abstracted class */
public class Car {
   
private Engine engine;
   
   
public void startCar(){
       
combustFuel();
           
useBattery();
           
sparkEngine();
   
}
   
public void accelerate(){
       
burnFuel();
           
increaseTorque();
   
}
   
public void stop(){
       
stopFuel();
           
useAirLocking();
           
increaseTireTraction();
   
}
   
/** Call combustfuel, useBattery, sparkEngine to start the Car */
   
private void combustfuel(){
       

   
}
   
private void useBattery(){
       

   
}
   
private void sparkEngine(){
       

   
}
   
/** Call burnFuel, increaseTorque for Acceleration */
   
private void burnFuel(){
       

   
}
   
private void increaseTorque(){
       

   
}
   
/** Call stopFuel, useAirLocking, increaseTireTraction for Braking */
   
private void stopFuel(){
       

   
}
   
private void useAirLocking(){
       

   
}
   
private void increaseTireTraction(){
       

   
}
}
Advertisements

About Sharath

I am a passionate technologist, I have varied interests in Development, Testing and Maintenance phases of PDLC / SDLC. In my leisure I love to play with my daughter. I love eating out, hanging out with friends and reading blogs/articles.
This entry was posted in JAVA, OOPS and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s