开发者

encapsulation and abstraction OOPs concept

开发者 https://www.devze.com 2023-01-19 07:44 出处:网络
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

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??


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.


Encapsulation means hiding the implementation

Abstraction means providing blueprint about the implementation

Data Hiding means controlling access to DataMember or attributes


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.


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.


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

Abstraction refers to representing essential features.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号