use of private in place of public | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

use of private in place of public

If we use private in place of public wt happend

14th Aug 2017, 2:53 AM
ROSHAN SHETTY
ROSHAN SHETTY - avatar
5 Answers
+ 3
private - No other class can access it. public - every class can access it. Example: public class A { private int x = 1; // this should be public } public class Program { A a = new A(); a.x += 2; // Error because x is private }
14th Aug 2017, 3:05 AM
qwerty
qwerty - avatar
+ 19
private attributes of a class can only accessed fromm their own methods. But you have 3 types of classes public, protected and private. Maybe some links will help: https://stackoverflow.com/questions/1020749/what-are-public-private-and-protected-in-object-oriented-programming https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables The most other sites I have found are very specific to the language...
14th Aug 2017, 10:00 PM
Worm
Worm - avatar
+ 2
your methods are not easily accessed
14th Aug 2017, 2:56 AM
Guilherme Ferreira Carvalho
Guilherme Ferreira Carvalho - avatar
+ 2
make all instance variables private. use getters to access them. don't set instance to public, you can access them that way but it is absolutely disgusting, like watching kittens being stabbed to death
14th Aug 2017, 5:52 AM
Gao Xiangshuai
Gao Xiangshuai - avatar
+ 1
Tq
15th Aug 2017, 6:09 AM
ROSHAN SHETTY
ROSHAN SHETTY - avatar