tags:

views:

164

answers:

4

I am confused about abstraction and encapsulation.What i feel is that class is encapsulation as it encapsulates data and behaviour,while interface is abstraction.Please comment

+2  A: 

Look at the Wikipedia article on abstraction in Computer Science. In particular, you are probably interested in abstraction with respect to object-oriented programming. I've quoted part of the relevant section below:

In object-oriented programming theory, abstraction involves the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system. The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction. When abstraction proceeds into the operations defined, enabling objects of different types to be substituted, it is called polymorphism. When it proceeds in the opposite direction, inside the types or classes, structuring them to simplify a complex set of relationships, it is called delegation or inheritance.

Generally, I would say that interfaces AND classes should be examples of abstractions, dealing with the data as a conceptual "thing" rather than raw data. Encapsulation is used to make these abstractions work well with other abstractions of conceptually different "things."

tvanfosson
+7  A: 

I think your terminology is confused.

  1. Encapsulation - puts related data and functionality in one place. We can get this through classes

  2. Polymorphism - Allows values of different data types to be handled using a uniform interface.

Polymorphism can be achieved by inheriting base classes (with virtual functions) and/or by implementing interfaces.

These techniques (and others) give us abstraction, which really applies to any of the processes we use to break a problem up into smaller components.

EDIT

Q) You ask "Can i say,abstraction is the topmost hierarchy which is accomplished through encapsulation and polymorphism?"

A) I can't answer that question, I don't know what you mean by "topmost" and "highest". There is no hierarchy here.

Functional Decomposition is a form of abstraction, it can be achieved without using Object Orientation, where should it come in the hierarchy?

The best I can do with a hierarchy is this definition (straight out of my own head, so YMMV)

  1. Abstraction is the practice of breaking a large problem into smaller components, so each smaller problem can be worked on in (relative) isolation.
  2. Polymorphism is a technique we can use to achieve abstraction. It involves identifying different types of data and behavior that can be treated in an homogeneous manner.
  3. An interface only declares types of behaviour, encapsulating the behaviour in a type. It provides no actual behaviour or data
  4. An abstract class declares types of behaviour, but may also provide behaviour and data, all encapsulated in a type.
  5. Therefore, an interface can be seen as providing a simpler or purer form of polymorphism than abstract classes.
Binary Worrier
Can i say,abstraction is the topmost hierarchy which is accomplished through encapsulation and polymorphism.
Rohit
This clears each and every thing.Thanks man.
Rohit
You're welcome, thanks.
Binary Worrier
+1  A: 

The purpose of encapsulation is to hide the implementation details:

A highly contrived example:

public class Person {
  private int age;

  public boolean canBuyBeer() {
    return age >= 21;
  }

}

you might later change this to:

public class Person {
  private int age;
  private boolean isInUSA


  public boolean canBuyBeer() {
    if( isInUSA )
        return age >= 21;
    else
         return age >= 18;
  }

}

The rules regarding age and origin can change but the caller doesn't need to know.

Interfaces can be used to abstract out different types. Consider this:

public interface Beverage {
  public boolean containsAlchohol;
}

public class Soda implements Beverage {
  public boolean containsAlchohol {  
      return false;
  }
}
public class Beer implements Beverage {
  public boolean containsAlchohol {
       return true;
  }
}

You might update Person like:

public class Person {
  private int age;
  private boolean isInUSA


  public boolean canBuyBeverage(Beverage b) {
    if( b.containsAlchohol() ) {
       if( isInUSA )
           return age >= 21;
       else
           return age >= 18;
    }
    else {
        return true;
    }
  }

}

So Person is encapsulating the logic if when a Person can buy a certain type of beverage. The Beverage Interface abstracts out the logic associated with a Beverage.

Better examples can be had, but that is the general idea.

Dave
+2  A: 

I think you've got it about right. An interface says, broadly: this is what this kind of thing is. A class says: here's how this thing works; I'll hide the details inside myself. A class is a (usually) concrete implementation of the interface abstraction.

Carl Manaster