+ 2

Why Private, Public etc... is needed?

What it is helping us with?

7th Dec 2017, 8:39 PM
Guy293
Guy293 - avatar
4 Réponses
+ 30
They are hiding data from anyone who is going to use your code. And you need getters and setters to control the access to the data.
7th Dec 2017, 8:49 PM
Igor Makarsky
Igor Makarsky - avatar
+ 10
You're hiding it mostly from yourself. Imagine you write a super complicated Chat class that does lots of different things like running a chatting server, receiving messages from others, writing things to files and databases, etc. And you only make the "sendMessage" function public. That way, you have lots of advantages: - If you need to change any of the complicated stuff you can just do so, because you know no other code depends on it - You know the private parts (heh) of your class can't be touched so you don't have to worry about other code setting your important variables to null or whatever. Usually, the smaller the "surface area" of your class is, the easier it is to handle. OOP is all about abstracting, so if you only have a single public "sendMessage" method, you can "forget" about how the chat works internally and just use it as this magical chat blackbox that sends messages. This frees your mind to think about other things.
7th Dec 2017, 8:57 PM
Schindlabua
Schindlabua - avatar
+ 3
They are access modifiers! Try reading the course,youll get it
7th Dec 2017, 8:40 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 1
hiding the data from access by another object/function. a very common but useful example: you wouldn't want someone to get access to your bank account fro. his account?
7th Dec 2017, 8:52 PM
storm
storm - avatar