Difference between Abstraction and encapsulation in Java

Abstraction and encapsulation are two totally different concepts in JAVA.

Encapsulation is technique by which we can hide the data with in a class and provide public methods to manipulate the hide data.

To achieve encapsulation we can declare variables private and provide public methods to manipulate these private variables.

In encapsulation object work as a container or capsule for class related private variables and methods.

Abstraction and encapsulation

Abstraction is a technique that is used to present common functionality among a set of classes. If we take an example of Automobile domain, as we know every vehicle Car, Bus, Truck etc. have some common functionality which we can achieve by using super class. If ignition is common functionality between all vehicles than other vehicle do not need to implement this functionality they can get this functionality by super class.
Abstraction in Java is represented by Interface, Abstract class, Abstract methods using "abstract" keyword. It hides certain details of the object that are not important and display that are essential. Encapsulation solves the problem in implementation level while the Abstraction solves the problem in the designing level.

Abstraction solves the problem in the design side while Encapsulation is the Implementation.

Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.

Comments

  1. By Muhammad javed

    Reply

Leave a Reply

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