Home > Enterprise >  Program without and with data hiding in java
Program without and with data hiding in java

Time:04-01

I have studied about data hiding in java theoritically but dont know what is happening inside. In every tutorial , stating that unauthorized person cant access data of others .

Can anyone please give example what will happen without and with data hiding with two or three users programatically.

CodePudding user response:

Data Hiding is hiding internal data from outside users. This is achieved by making the attributes of your class private and not letting the objects of the class access it directly, instead we create getters and setters to access the private attributes. Example:

//Without Data Hiding

public class Model{
public String name;
}

public class JavaApp{
public static void main(String args[]){
Model mObj = new Model();
mObj.name="abc";     // name = "abc"

}
}

//With Data Hiding

public class Model{
private String name;   //private name
}

public class JavaApp{
public static void main(String args[]){
Model mObj = new Model();
mObj.name="abc";     // Error
}
}

CodePudding user response:

Blockquote

Data hiding is a technique of hiding internal object details, i.e., data members. It is an object-oriented programming technique. Data hiding ensures, or we can say guarantees to restrict the data access to class members. It maintains data integrity.

  • Related