What is the difference between abstraction and data hiding




















Getter is used to accessing the private data and setter is used to modify the private data only after authentication. In simple terms, it is hiding internal data from outside users.

Concept involved in data Hiding: Getter and setter Getter is used to accessing the private data and setter is used to modify the private data only after authentication.

Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article.

String name;. Data hiding helps to protect the members of a class. Programming languages such as Java have access modifiers like public, private and protected. Objects of other classes can access public data members and methods. However, protected members are accessible by the objects of the same class and its subclasses.

Moreover, private members are only accessible within the class. Therefore, these access modifiers help to secure the members. When it is necessary to restrict objects of other classes from accessing the members of a class, the programmer can use the private access modifier.

Then, the data members are only accessible within the class. It is also possible to make the data members private and allow accessing them through public methods getters and setters. Encapsulation is a major concept in OOP. It wraps the data members and methods into a single unit. Furthermore, it is a way of achieving data hiding.

InformationHiding is the idea that a design decision should be hidden from the rest of the system to prevent unintended coupling.

InformationHiding is a design principle. InformationHiding should inform the way you encapsulate things, but of course it doesn't have to. Both Abstraction and Encapsulation are two of the four basic OOP concepts which allow you to model real-world things into objects so that you can implement them in your program and code. Many beginners get confused between Abstraction and Encapsulation because they both look very similar. If you ask someone what is Abstraction, he will tell that it's an OOP concept which focuses on relevant information by hiding unnecessary detail, and when you ask about Encapsulation, many will tell that it's another OOP concept which hides data from outside world.

The definitions are not wrong as both Abstraction and Encapsulation does hide something, but the key difference is on intent. Abstraction hides complexity by giving you a more abstract picture, a sort of 10, feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.

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.

I too was very confused about the two concepts of Abstraction and Encapsulation. But when I saw the abstraction article on myjavatrainer. Encapsulation is how the object is created, and abstraction is how the object is viewed in the outside world. Encapsulation: binding data and the methods that act on it. Abstraction: is hiding the non relevant behavior and data. How the items are actually stored, added, or deleted is hidden abstracted.

My data may be held in simple array, ArrayList, LinkedList, and so on. Also, how the methods are implemented is hidden from the outside. Encapsulation- enforcing access to the internal data in a controlled manner or preventing members from being accessed directly.

Now, whenever a user uses the above method he will just get the area not the way how it is calculated. We can consider an example of println method we just know that it is used for printing and we don't know how it prints the data.

I have written a blog in detail you can see the below link for more info abstraction vs encapsulation. How are we doing?

Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 13 years, 2 months ago. Active 1 year, 5 months ago. Viewed k times. Improve this question. Community Bot 1 1 1 silver badge. Add a comment. Active Oldest Votes. Grady Booch says in Object Oriented Analysis and Design, page 49, second edition : Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object Improve this answer.

Narendra Pathai Sure, encapsulation is achieved through information hiding, but Another real world example for Booch's definition is available here — lifebalance. Could you please tell me if it would correct to say that Abstraction is best understood through the Client code's perspective while Encapsulation is best understood through from the Service Code i. Real World Example for above definition is explained here: stackoverflow. An abstraction concentrates on external aspects of an object.

Abstraction provides the separation of this crucial behaviour from its implementation. It specifies a conceptual border relative to the outlook of the viewer. A suitable abstraction highlights on the details that are important for the user or reader and simplifies features which are, irrelevant and deviant.

User-defined data types are created by specifying abstract attributes and functions inside a class for designing real-world objects, having properties that are alike.

These attributes are called data members because they include the information. Likewise, the functions that operate on these data are called member functions.



0コメント

  • 1000 / 1000