Abstraction,Encapsulation and Data hiding

Abstraction,Encapsulation and Data hiding are all interconnected concepts in OOPS(Object Oriented Programming System).Java programmers are mostly confused when they are asked about the difference between abstraction,encapsulation and data hiding.

Abstraction is all about showing the necessary features and hiding the unnecessary background details from the outside world i.e., the client class.It’s about showing a simple view of a complex system. For example, while developing a Calculator program, we will only expose the APIs like addition,multiplication,division etc. and just return the results. Here we will hide the background details about how these APIs internally work.

Encapsulation is just wrapping the data  and behaviour into a single unit.It is about the internal implementation on how we bind everything into an object whereas abstraction is the outside representation of the object.In other words,Encapsulation is the mechanism by which Abstraction is implemented.Abstraction focuses on the observable behavior of an object. Encapsulation focuses upon the implementation that give rise to this behavior.

AbstractionEncapsulation
What are the essential features which needs to be exposed to outside world.How to implement those essential features.
Use Interface and Abstract classes to expose the necessary features.Use concrete classes to implement the necessary features.
Example:

package com.car;

public interface Car {

public void start();

public void drive();

public void stop();

}

Example:

package com.car;

public class CarImpl implements Car{

@Override

public void start() {

System.out.println(“Car Started”);

}

@Override

public void drive() {

System.out.println(“Driving Car”);

}

@Override

public void stop() {

System.out.println(“Car Stopped”);

}

}

Data Hiding – It is the process of hiding the implementation details of an object. It is a result of Encapsulation.The focus of data encapsulation is on the data inside the capsule while data hiding is concerned with restrictions and allowance in terms of access and use.

Abstraction, information hiding, and encapsulation are very different, but highly-related, concepts. Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden. Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible and hence encapsulation keeps the data safe from outside interface and misuse.

Also Read: 

Leave a Reply

Your email address will not be published. Required fields are marked *