+ 4
Why do we declare private variable?
if you say for security, then how it will make an application secure. what will happen if i declare every variable public.
11 Antworten
+ 9
Private makes it visible only to that class in which it is declared, you cannot access it through any other class. Public is just the opposite, it can be accessed from anywhere outside your class.
For a better explanation, please refer:
https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables
+ 9
@Somnath: Did you read the answers in that link above? If you've, you may not be asking "so???"
”Private variables help prevent people from depending on certain parts of your code. For example, say you want to implement some data structure. You want users of your data structure to not care how you implemented it, but rather just use the implementation through your well defined interface. The reason is that if no one is depending on your implementation, you can change it whenever you want. For example, you can change the back-end implementation to improve performance. Any other developers who depended on your implementations will break, while the interface users will be fine. Having the flexibility to change implementations without effecting the class users is a huge benefit that using private variables (and more broadly, encapsulation) gives you.”
+ 7
@Somnath
Though users can not write code in the app , someone might edit the source code of the app and call the methods there . So it is important to make some variables private
Note : The methods we are calling on the app are used to change data on the main server , so anyone can not just edit the main code and make the variables from public to private
+ 4
Why C tag ?
+ 3
I guess you dont understand how it helps making codes secure
To do anything we need classes
For example - lets take a code for a bank where there are few classes like class Owner , class Client
Owner class is the main class which has functions like close Account , etc
Client has data about client
Every time a new client opens his account with the bank , new object of client ( new Client(); ) is created which contains all the information about that client .
The Bank has made an app where they allow users to perform methods of their client class , like check balance , withdraw/send money , etc.
So if the methods or variables of the Owner class were declared public instead of private , that would mean that any other class can use the functions of Owner class .
Since Owner and Client class are part of the same source , therefore Client class would be able to use functions of Owner class
Clients would be able to call closeAccount(id) method by typing Owner.CloseAccount() .
People can misuse it and close accounts of other people , this function is supposed to be used only by the owner of the bank not by every client
Therefore assigning a variable/method private limits other class from using data they aren't supposed to use
Boy , That was a long answer
+ 2
thank u utkarsh..still i couldn't understand some points like...u said
The Bank has made an app where they allow users to perform methods of their client class...here user will not write code..if the bank make the whole application,then they should not have any problem with public variables. from your point, i got an idea. like if RBI make any library and they share with every bank. and if they don't want not to share any method or variables with any bank..then they will make all variables private
+ 2
@Sommath yes, but they can set restrictions to the value that can be set
+ 1
Utkarsh great..sorry i am late..I got ur point..but i have a question..how can someone edit main code..if they don't allow..i did understand that you can change the value of the variable..if they give access for some function/functionality then you can change the value...
0
so???
0
public vars..means any body (object)can access and/or alter it...so a genius came up with the idea of private ones...you can access your variables and define functions (like getters and setters) to define how people get to your members and what can they do with it
0
So that users can't mess with them. It's OOP.