-2

I am confused more and more when reading definitions of encapsulation and abstraction in different sources. It is really hard to decide which is the correct definition.

Encapsulation definition 1

Encapsulation : Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods

Encapsulation definition 2

Encapsulation is the procedure of covering up of data and functions into a single unit (called class).

Abstraction definition 1

Abstraction is a way of modelling a real world object with minimal information.

Abstraction definition 2

Exposing only the methods which are necessary.

Please help me in understanding the correct definition of these words mean related to OOP. A source to a book which explains the correct definitions would also be preferred.

DesirePRG
  • 6,122
  • 15
  • 69
  • 114
  • 4
    There's no one correct definition. Each version you gave talks *about* encapsulation/abstraction, but does not authoritatively *define* it. – Marko Topolnik Mar 02 '15 at 18:28
  • possible duplicate of [How encapsulation is different from abstraction as a object oriented concept in java?](http://stackoverflow.com/questions/8960918/how-encapsulation-is-different-from-abstraction-as-a-object-oriented-concept-in) – Tachyon Mar 02 '15 at 18:30
  • 1
    Since these are concepts, there is no specific definition that is globally accepted. For instance, most people would view the two definitions for Encapsulation in your question above as equivalent... Learn to use Google and Wikipedia ... they are your friends. – RudolphEst Mar 02 '15 at 18:32

3 Answers3

2

Talking about code, an abstraction is simply a piece of code that describes a real world "thing". Most times you can't describe the whole "thing", but you can describe it's main characteristics, which, in your code, would be called attributes.

One example of an abstraction would be a Restaurant: You go, you ask for the menu and select whatever you want to eat... but you don't eat the paper: You select what you want looking at an abstraction of it (the menu)... that abstraction may just say "Eggs & Bacon", but when the waiter brings your order it has a pair of eggs (scrambled), two pieces of crispy bacon and some other things... The menu is just a simple abstraction of the real thing.

Take the Bicycle example in the Java Tutorials: The bicycle has attributes:

  1. Speed
  2. Gear
  3. Cadence

You're not interested (in this example) in any other characteristics of the bicycle... so this is an abstraction.

Now, about encapsulation: when writing code, you want your object's attributes to be modified only in a "propper" way: If you change the speed (again the Bicycle example) you may have to also change the cadence and/or change the gear. So you declare the attributes private and update them only through public methods, and, in these methods, you ensure that all the attributes have coherent values:

public class Bicycle {
    private int speed, gear, cadence;
    /**
     * Default constructor
     */
    public Bicycle() {
        speed = 0;
        gear = 1;
        cadence = 0;
    }
    /**
     * An example 
     */
    public void setSpeed(int speed) {
        this.speed = speed;
        /*
           You can set the value of the other attributes here if you want to
         */
    }
    /**
     * Another example
     */
    public void gearUp() {
        if(gear < 5)
            gear++;
        /*
           Here you are preventing other methods to set the gear to
           a value higher than 5.
         */
    }
    /**
     * Another one
     */
    public void gearDown() {
        if(gear > 1)
            gear--;
        /*
           Here you are preventing other methods to set the gear to
           a value lower than 1.
         */

    }
}
Barranka
  • 20,547
  • 13
  • 65
  • 83
2

Abstraction
Abstraction doesn't have to do with making methods private or public. It has to do with recognizing what kinds of functionality are common to a type of class.

In java an abstract class takes one step further than an interface. In interface says classes of type X have to do X,Y,Z. X,Y,Z can be implemented anyway you choose.

An abstract class says classes of type X have to do X,Y,Z but a method marked abstract has to be done a particular way, thus it's implemented in the abstract class (abstraction).

Encapsulation
It's just wrapping one or more things in another thing. To simplify or to hide code. One example of why you would want to do this is to simplify a flow. If you want a user to be able to shut down a computer for example you can could either have a bunch of method for someone to call explicitly:

Util.shutdownApps();
Util.shutdownServices();
Util.shutdownScreenAndKeyboard();
Util.poweroff();

If you want to shut down in a particular way or just make it easier for someone to shut down you could encapsulate it:

Util
    public void shutdown(){
    Util.shutdownApps();
    Util.shutdownServices();
    Util.shutdownScreenAndKeyboard();
    Util.poweroff();
    }

  private shutdownApps(){...}
  private shutdownServices(){...}
  private shutdownScreenAndKeyboard(){...}
  private poweroff(){...}
Savan
  • 21
  • 2
  • regarding Barranka's answer, any piece of code can describe a real world thing. There is not point in having an abstract class if you're only going to have *one* of that type. Someone i'm sure will say oh but having an interface would be better for testability. But if you have one why make extra code? You can refactor when you figure out you need different similar types. – Savan Mar 02 '15 at 18:59
0

Take for instance the two code snippets below:

// Snippet #1
public static void Main(){
    int sum = 0;
    for (int i = 0; i <= 1000; i++) {
        sum += i;
    }
    System.out.println(sum);
}

In the snippet above, you the program is computing the sum of all the integers from 0 up to 1000. You can see how the computation is done, and you have control over the variable that stores the value of the sum.

// Snippet #2

public class Adder {
    private int sum;

    public Counter(){
        this.sum = 0;
    }

    public void add(int value){
        this.sum += value;
    }

    public int getSum(){
        this.sum;
    }
}

public static void Main(){
    Adder adder = new Adder();
    for (int i = 0; int <= 1000; i++) {
        adder.add(i);
    }
    System.out.println(adder.getSum());
}

The snippet above does the same task, but differently. There is a class that provides the two characteristics you were questioning about. It provides is an abstraction to systematically add numbers, and also encapsulates the details of how the computation is made. In other words, it separates the concern of adding numbers and how it works from the task of iterating the numbers from 0 to 1000 (in the Main function).

In this example:

  • abstraction is the concept of adding numbers, so you would have a different abstraction for each different type of idea

  • encaptulation is the separation of the details of implementation from the main program. That is, you could create a new class for the same type of abstraction but uses a different implementation (a long rather than un int, for example).

thalesmello
  • 3,301
  • 3
  • 20
  • 20