What is the difference between hiding information and encapsulation?

I know that there is a difference related to research, but I can only find the similarities between them ... I was hoping that someone would clarify the difference, and if you can give an example for everyone, this will really help. A Java program, please, will also consider this program encapsulation or hidden information, or even both.

class DogsinHouse { private int dogs; public int getdog() { return dogs; } public void setdog(int amountOfDogsNow) { dogs = amountOfDogsNow; } } 
+5
source share
1 answer

The part of the code that you publish is an example of both. Encapsulation is a method for which the Java class has state (information stored in the object) and behavior (operations that the object can perform, or rather methods). When you call a method defined in class A in class B, you use this method without knowing its implementation, just using the open interface.

Information Hiding the principle for which istance variables are declared private (or protected): it provides a stable interface and protects the program from errors (as a modification of a variable from a part of the code that should not have access to the aforementioned variable).

Basically:

Encapsulation using information hiding:

 public class Person { private String name; private int age; public Person() { // ... } //getters and setters } 

Encapsulation without hiding information:

 public class Person { public String name; public int age; public Person() { // ... } //getters and setters } 

In OOP, it is good practice to use both encapsulation and information hiding.

+2
source

Source: https://habr.com/ru/post/1237076/


All Articles