Tuesday, August 28, 2012

Difference between Abstraction and Encapsulation in Java - OOPS


Abstraction vs Encapsulation – Java OOPS
Abstraction and Encapsulation in Java are two important Object oriented programming concept and they are completely different to each other. Only similarity between Abstraction and Encapsulation is that they are OOPS concept, other than that they mean two different things. Abstraction represent taking out the behavior from How exactly its implemented, one example of abstraction in Java is interface while Encapsulation means hiding details of implementation from outside world so that when things change no body gets affected. One example of Encapsulation in Java is private methods; clients  don't care about it, You can change, amend or even remove that method  if that method is not encapsulated and it were public all your clients would have been affected. Apart from this main difference in behavior here are couple of more differences between Abstraction and Encapsulation in Java.

Abstraction vs Encapsulation in Java

Difference between Abstraction and Encapsulation in JavaHere are some of the main differences between Abstraction vs Encapsulation in Java and OOPS(Object Oriented programming) concept. Abstraction and Encapsulation along with Inheritance and polymorphism forms basis of Object oriented programming in Java.


1) First difference between Abstraction and Encapsulation is that, Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.

2) Encapsulation is also called data hiding.

3) Design principles "programming for interface than implementation" is based on abstraction and "encapsulate whatever changes" is based upon Encapsulation.

That's all from my side on differences between Abstraction and Encapsulation in Java. Correct understanding of Encapsulation and Abstraction is must for any Java developer. Head first Object oriented Analysis and design is a great book to learn more about Abstraction, Encapsulation and other OOPS concept.


Other Java programming and OOPS tutorials

10 comments:

  1. This is nonsense. Your definition of abstraction and encapsulation are the same.

    ReplyDelete
    Replies
    1. How come definition is same, Its clearly saying that abstraction is implemented using interface and abstract class and Encapsulation is implemented using private keyword and other access modifier.

      Delete
  2. need more clear concept about abstraction...

    ReplyDelete
  3. examples explaining both concepts would be helpful.

    ReplyDelete
  4. Well Abstraction and encapsulation are both viewing same thing in java with different point. here abstarction means showing functionality hiding information i.e complexity and in encapsulation u do same thing u wrap up things within a class which will encapsulates. wat i can say is abstraction is evalved from encapsulation by its implementation and abstraction by its use. i.e encapsulation is part of abstraction. Ex: creating a class is encapsulation and way of implementation i.e by using intereface and abstract classes is abstraction.

    ReplyDelete
  5. Well Abstraction is the concept and encapsulation is mechanism to fulfil that concept :)

    ReplyDelete
    Replies
    1. well said sir...

      Delete
    2. encapsulation is not a mechanism but a principle or conecpt like abstraction

      Delete
  6. I think following link
    http://lavraham.com/node/21 defines what pradip says more clearly.

    ReplyDelete
  7. but how they are complementary to each other????

    ReplyDelete

Java67 Headline Animator