The question is this : why would you sometimes decalre the variables i.e. lcdField as private and sometimes declare it as public??? What do you achieve by doing it either private or public.
If you declare a variable as "private" to the Calculator class, it can only be accessed by objects that is of the type Calculator class. If you declare the variable as "public", then it can be accessed by objects of any type. (There is also the "protected" declaration)
Anyway. Why would you want to do this? The "private" keyword makes the class restrictive and only makes it harder to code, since you don't have all the information available to the rest of the program, right?
Actually, you want to have as little public variables and methods exposed as possible? Why?... Let's say you need to add functionality to the calculator class, and you need to change some of the variables and methods. For "private" variables, you are sure that it is only accessed by the calculator class. As long as you don't change the signature of the original public interface, it will work with all other code.
If you had made it public, you would have to inform every person that used your calculator class, to look into every usage to make sure that everything still works, because you changed an internal variable.
Hmmm... this explanation turned out to be longer than expected...
No, he's saying the opposite: wherever possible, variables and methods should be private.
In fact, at first you could even write all of them as private and then when you add other classes, make public only the ones that the other classes need to access.
Joined: Jul 23, 2004
I've Got it.
It's funny because the course clearly states to make all the varaibles public, and I can't understand why.
But I understand now.
many Thanks Dave Morley
Joined: Apr 21, 2005
It may be telling you to make them public to make things easier for now, with the intention of making them private and using more methods later. Kind of using public variables, demonstrating the problems they have, and then moving on to the better solution. I have seen several courses like that.