• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

private, public, and protected keywords?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why they are private , public , protected keyword useful , i know that private modifiers you can access only in that class public you can access in all other classes what are benefits of hiding variable and not sharing with other classes why not making all the variables public and that's it .
Does it affect in performance when you make the variables private or protected ?
Or it is dangerous to make the variables public because someone can hack or break something ( sorry if this is very stupid question) ???

Sorry if this question is very stupid , i am bad student and i have decide to learn

Thank you
 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only stupid questions are those that don't get asked!

I struggled with this a bit myself when I first started. The best way I can explain is to use a real-world example.

Your computer is made up of all different parts. It has a processor, a video card, a motherboard, etc. All those parts are neatly encapsulated; in other words, you don't have all these parts sticking out. You don't have to go and connect loose wires together to start your computer. Everything is hidden inside the computer case, and you are given a neat little button to push to turn the computer on.

Turning that computer on is more complicated a task than you might imagine. All the different electronics and circuits (which themselves are encapsulated) are getting just the right amount of power to do what they need to do. You don't have to be an electronic engineer to work a computer. You just have to use the interface that the computer gives you. That interface is a button that, when pushed() turns that computer on or off.

Suppose those wires were exposed tho, and you had to connect them manually. Not only is there a bigger chance of making a mistake (crossing the wrong wires) but there's a bigger chance of an accident (two wires getting crossed that weren't supposed to be crossed). This could potentially damage the computer (or the user for that matter )

When we write object oriented programs, we want our code to act like individual pieces of a whole thing. Just like the computer is made up of parts, we want our program to be made up of parts. And each of those parts might be made up of other smaller parts. We don't want our wires hanging out all over the place, because accidents could happen, or the user could cross the wrong wires and make our program do weird things!

Using access modifiers like public, private and protected, we can help to prevent weird things from happening by only allowing what we want to happen.

example:



If another part of your program wanted to use the class Unchanged, the only thing they would be able to do is read the variable youCantChangeMe by using the readVariable() method. If they tried something like



That would be an error, because youCantChangeMe is private. This essentially protects your variable from being changed without your knowledge, and prevents hard-to-find bugs.

protected gives package-level access and if you're just starting don't worry too much about it right now. Just use private and public. If the class your writing is the only thing that should be manipulating something, keep it private. If you need to give access to data outside your class, use accessor methods (set() and get()).

Hope this helped you out a bit. Keep learning and asking!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bad Student wrote:
Does it affect in performance when you make the variables private or protected ?
Or it is dangerous to make the variables public because someone can hack or break something



No, it's not about performance, and it's not about hackers. It's about making the design clear and understandable, and making the class easy to use.

When we design a class, it is there to perform a specific job. Users of that class need access to certain parts of it in order to get it to do its job. Most classes also have other parts that they need to be able to do their jobs, but that the clients of that class don't need access to. Making everything wide open and easily available would make it harder for a programmer looking at that class to know how it's intended to be used, and it would be easier for him to accidentally use it in a way it's not meant to be used, possibly causing it to stop functioning correctly.

For instance, an ArrayList has an array to store its elements. The array starts off with every element null, and as we all the add() method, the elements we passed are assigned to index 0, then index 1, 2, and so on. The ArrayList class has a private variable called count or size or somethign that it uses to keep track of how much of the array we've used up so far. The only time that variable should be changed is as part of operations like add() and remove(). The ArrayList class knows exactly when and how to change that variable. Client code has no need to see it or modify it directly. Because it's private, the ArrayList class can make sure that is always has the correct value, to reflect what's been put into and taken out of the list. If we had access to it, we might change it ourselves, when we have no reason to, putting the list in an invalid state.
 
Arbnor Qeku
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys
reply
    Bookmark Topic Watch Topic
  • New Topic