-1

I have studied data hiding in java theoretically but don't know what is happening inside. Every tutorial, states that unauthorized persons cant access the data of others.

Can anyone please give an example of what will happen without and with data hiding with two or three users programmatically?

Sibin Rasiya
  • 1,132
  • 1
  • 11
  • 15
SMValiAli
  • 85
  • 1
  • 6
  • I hope https://docstore.mik.ua/orelly/java-ent/jnut/ch03_05.htm will answer your question. – Sibin Rasiya Apr 01 '22 at 07:43
  • Thank you but I need two or three users who tries to access same data . I have read many tutorials but couldnot understand. – SMValiAli Apr 01 '22 at 08:34
  • Want a sample example of banking with and without data hiding please help me to understad. – SMValiAli Apr 01 '22 at 08:35
  • 1
    I am not sure what you are asking for. Imagine you walk into your bank, you show your passport, and then the guy behind the counter tells how much money you got in your account, and allows you to take some of it. Would he tell you whether your neighbour has an account there, or how much money is in that account, or to take money from that account? Nope. And any real IT system does the same. But note: "data hiding" is a much more "low level" concept. There we simply talk about the HIDING of "internal status" of objects of some class. That doesn't really correlate to your request about ... – GhostCat Apr 01 '22 at 09:33
  • multiple users and such. You are somehow over- and underthinking this at the same time. – GhostCat Apr 01 '22 at 09:34

1 Answers1

0

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
}
}
mukta
  • 1
  • 1