0

Possible Duplicate:
difference between abstraction and encapsulation?
What is the difference between abstraction and encapsulation?

I've noticed that the notion of Encapsulation and Abstraction are important when discussing OOP design. However, I'm not too sure on how they differ from one another. I've read a bunch of articles and posts (of which most of them are from this site) which merely contradict each other.

It's my understanding that Abstraction is a means of decomposing the complexity of real-world objects. This decomposition may involve the creation of several objects that collectively represent a real-world object. Abstraction can also have many layers to it, class inheritance is an example of when this would be the case.

Encapsulation is the process of hiding away the inner implementation data within a class. Language features such as Access modifiers and Accessor methods are key in this.

If these definitions are true (correct me if I'm wrong), under what principle would Interfaces be used for? Abstraction? because a group of real-world objects can be summarized in an interface? Or Encapsulation, where the Interface itself can be used to expose the features of an object without revealing the inner functionality.

If possible could you also provide an brief example, that will clearly distinguish these two principles?

Cheers.

Community
  • 1
  • 1
Nick
  • 1,269
  • 5
  • 31
  • 45
  • 3
    Please see [Lesson: Interfaces and inheritance](http://docs.oracle.com/javase/tutorial/java/IandI/index.html). – user1329572 May 16 '12 at 16:23

2 Answers2

0

The best way to understand Abstraction at least for me it's with this example think a moment in a geometric figures. Let's say triangle, square, rectangle and circle, this elements that form parts of this "Geometric Figures" if you want to represent this in a class model you could do something like this

public abstract class GeometricFigures
{

    public int perimeter(); //Common Behavior
}

Now this is an abstract class because this element don't have a real representation on our world, and that's ok because the GeometricFigures class cannot be instanced.

Never less you could define the child of this class for example trigule and this class inherits from GeometricFigures like this

public class Triangle inherits GeometricFigures
{
    public int size1;
    public int size2;
    public int size3;
    public int perimeter( int size1, int size2, int size3);
}

The Concept of Abstraction allows me to represent the father class GeometricFigures understanding that this class doesn't exit's in the reality

Jorge
  • 17,896
  • 19
  • 80
  • 126
0

interfaces belong under the abstraction category. They 'abstract' or factor out the common features of a class into another class, forcing all sub-classes to abide by their contract and implement all of its methods. In my mind I think of abstraction and interfaces as code-reuse which facilitates more maintainable code, whereas encapsulation primarily allows for greater measures of security. They are both major OOP concepts and do share some comparisons.

loveToCode
  • 139
  • 4