+ 2
Why Private, Public etc... is needed?
What it is helping us with?
4 Respuestas
+ 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.
+ 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.
+ 3
They are access modifiers!
Try reading the course,youll get it
+ 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?