3

Some people define abstraction as :

Abstraction is hiding the implementation details by providing a layer over the basic functionality.

Is it not part of encapsulation to hide implementation details from user of the object ?

Lets say Animal class has function eat(), then providing this interface to user of object is encapsulation or abstraction ? or providing function name to use is abstraction and the hiding of method implementation part is encapsulation.

I am really confused as in many places it's abstraction and many say implementation detail hiding is encapsulation?

In this SO question top answer :

abstraction = the object externally; encapsulation (achieved through information hiding) = the object internally.

So does this mean object interface exposure is abstraction and the data hiding inside object is encapsulation ?

Community
  • 1
  • 1
cruxion effux
  • 1,054
  • 3
  • 14
  • 27

2 Answers2

5

Is hiding implementation detail Encapsulation or Abstraction?

Abstraction is all about providing an additional layer with interfaces and abstract classes. This layer (interfaces and abstract classes) tells about what needs to be done, but NOT how. So hiding implementation is called as the abstraction.

The best example to understand the concept of abstraction is that all J2EE/JMS specifications provide abstractions (typically interfaces) to the application vendors and then these interfaces will be implemented by the different vendors (like Tomcat/JBoss/Weblogic/ IBM/etc..) with the actual definitions/behavior (called implementations) for the specifications.

Abstraction talks only about WHAT needs to be done and Implementation tells about HOW it should be done.

Abstraction provides the power of injecting the behavior at Runtime (which is Polymorphism). Now, taking Spring framework (or infact any DI framework like Guice, etc..) taking as example, the Spring DI container injects the provided bean (through xml or annotations) implementation object (implementation) at runtime to the given interface type (abstraction).

So does this mean object interface exposure is abstraction and the data hiding inside object is encapsulation ?

Yes, almost, in Java abstraction can be achieved using interfaces or sometimes using Abstract classes (like J2EE HttpServlet, etc..).

Now coming to the Encapsulation, it is all about providing/defining the right level of access for the classes/methods/fields (hiding/protecting the classes and class members). In Java, Encapsulation can be achieved using access modifiers (protected/private/public/etc..).

You can look here for understanding more on abstraction in Java and here for overriding (implementation) and hiding methods.

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • see answer 1 as well as 2 http://stackoverflow.com/questions/742341/difference-between-abstraction-and-encapsulation – cruxion effux Nov 06 '16 at 19:31
  • Thanks for answer.Please see I know how it's done in java but as in above Q's top answer, clearly they mention hiding implementation as encapsulation. Also clearly in this Q's top answer: http://stackoverflow.com/questions/16938667/how-abstraction-and-encapsulation-differ/ Also this Q's top answer: http://stackoverflow.com/questions/12072980/encapsulation-vs-abstraction-real-world-example – cruxion effux Nov 07 '16 at 07:30
  • I have seen in the link like what you mentioned – Vasu Nov 07 '16 at 07:32
  • The thing is you can put the things in different phrases, what it means by "Encapsulation is hiding the implementation" is that Encapsulation is about providing right level of access (protecting class members, members contains methods and methods contain implementation) – Vasu Nov 07 '16 at 07:37
3

As with a lot of things in programming, neither terms are well defined, both are subject to revision, and are not necessarily mutually exclusive. I know it's perhaps unsatisfactory, but I would resist a purely technical definition of these terms.

Abstraction has a quite good definition on Wikipedia. It is the practice of extracting the "essence" of something, leaving/hiding everything that is not directly needed. It's true, that in Java "abstract" methods or "abstract" classes (interfaces) exists, and these can be used to provide abstraction over a concept, however merely using interfaces does not automatically qualify as "abstraction" in my book. It is merely a technical tool to achieve it. On the other hand, it is possible to abstract concepts without abstract methods.

Encapsulation is a related concept, not quite the same, but not entirely independent. The Wikipedia definition is quite bad unfortunately, because it focuses on the language. The concept of encapsulation means to protect details inside a unit/method/class/module/etc. It is relevant for both the "outside" and "inside" of an object. This is the thing you don't have, if for a new "field" on some business object you have to modify multiple "layers" like persistence, business, gui, api, etc.

I tend to agree with the Booch quote you referenced, but the problem with it is that it is quite misunderstood what "essential characteristics" of an object are. Encapsulation would for example never allow direct access to internal fields of an object. This means getters (and especially setters) are a no-no. One can argue about "degrees" of encapsulation of course, but most of the business code I've seen at least fails encapsulation quite spectacularly.

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38
  • [Edward Berard](https://stackoverflow.com/a/68218220/2326961) defines abstraction as a *selection* technique (*what* information should be made visible/hidden) and encapsulation as a *presentation* technique (*how* the information should be made visible/hidden). – Géry Ogam Jul 02 '21 at 00:28