4

Does Encapsulation is information Hiding or it leads to information hiding??

As we say that Encapsulation binds data and functions in a single entity thus it provides us control over data flow and we can access the data of an entity only through some well defined functions. So when we say that Encapsulation leads to abstraction or information hiding then it means that it gives us an idea which data to hide and which data to show to users... coz the data that users cant access can be hidden from them thus encapsulation gives us a technique to find out what data to be hidden and what should be visible... Is this concept correct??

And what is the difference between information hiding and abstraction??

Mishthi
  • 1,659
  • 4
  • 15
  • 12

7 Answers7

3

Possible duplicate of the this

public class Guest {
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

See the above code, we have encapsulated the String name, we provide the access to it through public methods.

Say we have created object of Guest called guest. Then the following will be illegal.

System.out.println("Guests name  : "guest.name);

Access through public methods is what can only be done.

guest.getName();

Benefits of Encapsulation:

  1. The fields of a class can be made read-only or write-only.

  2. A class can have total control over what is stored in its fields.

  3. The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

Community
  • 1
  • 1
ivorykoder
  • 960
  • 3
  • 14
  • 31
  • The information is not so clear... so i just want to have more details about it – Mishthi Oct 11 '10 at 15:35
  • In the context of Information hiding and abstraction... And also whether encapsulation leads or it itself is information hiding – Mishthi Oct 11 '10 at 15:41
  • Encapsulation is not information hiding. Encapsulation is done to restrict the direct access to the instance variables. Information is not hid, instead provided by means of public methods which is called Encapsulation. – ivorykoder Oct 11 '10 at 15:45
  • And what is the difference between information hiding and abstraction is also not clear?? – Mishthi Oct 14 '10 at 10:51
  • Abstraction means hiding implementation specific details which is not information hiding. Now you get it? – ivorykoder Oct 17 '10 at 15:26
  • So if in abstraction also we are hiding details then this means we are hiding Information... Same as Information hiding... Are the two complementary to each other .. – Mishthi Oct 23 '10 at 14:24
  • Nopes, they aren't complementary to each other. I again say, Abstraction is not hiding information, it is hiding 'implementation' details. – ivorykoder Oct 23 '10 at 15:37
  • Thanx...So interfaces in Java is a way of providing abstraction?? Or they leads to information hiding?? Then in information hiding what we hide if we dont hide implementation details?? – Mishthi Nov 09 '10 at 13:42
2

Encapsulation means hiding the implementation

Abstraction means providing blueprint about the implementation

Data Hiding means controlling access to DataMember or attributes

Mendy
  • 99
  • 1
  • 9
1

Information is a more general term, hence, i believe, to say Encapsulation is Information hiding, will not be appropriate. I would say Encapsulation is Data Hiding.

Encapsulation means ...

  • Combining an Object's State & behavior (that operates on that State), in one single unit. This closely mimics a real world Object.

  • Hiding & Securing Object's State from accidental external alterations by providing a well-defined, controlled access (through behaviors).

In Java, the definition can be detailed out as ...

  • In Java, Classes and Enums are single units for implementing encapsulation. State is defined using variables (primitives, references to objects), and behavior using methods.

  • Data Hiding is achieved using private access specifier on variables (so that no one can access them from outside).

  • Controlled Access is achieved by providing Getters / Setters and/or business logic methods. Both Setters and other State affecting methods should have boundary condition checks for keeping the State logically correct.

1

Encapsulation talks about hiding data into something and give it a name ( private data members in a class - Car) and binding behavior methods with it which will mutate or provide access to those data variables.

Abstraction provides perspective of the client in abstract terms. As a concept or idea. Car is concrete entity where as Drivable, Trackable(which has position and can be tracked down) can be abstraction for Car for different clients.

You can check some real life examples of Abstraction and Encapsulation here.

Vishal Shukla
  • 2,370
  • 1
  • 12
  • 18
0

Encapsulation is a technique used for hiding properties & behavior of an object.

Abstraction refers to representing essential features.

Akash
  • 3
  • 2
0

Encapsulation - Work complete and door permanently closed. Get work benefits through method name. Abstraction - Work started and door temperately closed. Open and change work using overriding Key.

bommu
  • 1
  • 1
0

Both these OOP principles involve information hiding but are different.

Encapsulation involves restricting the direct access to the variables of the class by making them private and giving public getters and setters to access them. Purpose: This is done so that the members of the class cannot be accidentally manipulated (and thus corrupted) from outside.

Abstraction involves exposing only the relevant details to the caller while hiding other details (details of implementation). The client does not need to bother about implementation which may change later. Example: The caller will call the add method of List, the implementation of which may be ArrayList today but may change to LinkedList tomorrow. Purpose: This provides flexibility that tomorrow the implementation can be changed. Also, it simplifies the design.

Saurabh Rana
  • 119
  • 1
  • 16