Monday, 13 May 2013

Difference between Encapsulation and Abstraction in OOPS

Difference between Encapsulation and Abstraction in OOPS

Abstraction and Encapsulation are two important Object Oriented Programming (OOPS) concepts. Encapsulation and Abstraction both are interrelated terms. 

Real Life Difference Between Encapsulation and Abstraction

Encapsulate means to hide. Encapsulation is also called data hiding.You can think Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation. 

Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.

Implementation Difference Between Encapsulation and Abstraction

1.  Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.

2. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.

3. Example of Encapsulation

Class Encapsulation
    private int marks;

    public int Marks 
      get { return marks; }
      set { marks = value;}

4. Example of Abstraction

abstract class Abstraction
    public abstract void doAbstraction();

public class AbstractionImpl: Abstraction
    public void doAbstraction()
       //Implement it


  1. This comment has been removed by the author.

  2. really nice but want to add more.

    1) Encapsulation -
    -- By applying access specifier (Private,public)
    -- By making method access specifier public
    -- use the Java Beans Getter/setter method instead of constructor

    Idea is to go fort access of variable via method not directly. Which is violation of Oops Rule.

  3. Good and simple

  4. nice examples for understand the concepts :)

  5. Very nice and easy to understood by anyone

  6. Welcomes to google terminal keep sharing such ideas in the future as well.
    google snake this was actually EARN TO DIE
    what i was looking for,and i am glad to came here! AGE OF WAR 2
    Hi! I’ve been reading your blog for a while HAPPY WHEELS
    I want you to thank for your time of this wonderful read!!! Slitherio
    Amazing insight you have on this, Slither io This article always blew me BIG FARM

  7. Woա, sսperb blog layout! How ⅼong have you been Ьlogging
    for? you make blogging look easy. The overall look of уour
    web site is wonderful, as well as the content! I has some good article to share with you.. Check List Here
    obat mata lelah karena komputer
    cara menghilangkan cairan dalam perut
    Cara Mengobati Keputihan Disertai Sakit Pinggang dan Perut