DEV Community

Discussion on: The Difference Between Public and Private in Java

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

Ooh these are great questions. It's my mistake on the Wiper example. In an effort to provide a couple method examples, I neglected to provide a constructor. You can assume the array of wipers is defined (length could be anything), but I'll update the example to include a constructor.

To answer your second question, there's a bit more nuance. You're totally right in your example. If your final application is a graphical user interface (GUI), you might not care about public vs. private. After all, the only thing you're exposing is a button.

However, if you're going to release the underlying class structure to the public, you may want to limit which methods you expose. There are a few reasons for that. For one, hiding certain functionalities allows you to limit how much control the user has over the underlying structures. In other words, you protect them from themselves. The other reason is that you're free to rework the underlying structure without messing up your public interface. For example, maybe you create a new data structure which has an exposed sorting method. You're free to change how sorting is accomplished as long as it provides the same functionality to the user.

Also, I just think it's good practice regardless. Having a clean and clearly defined interface between classes makes for a lifetime of painless maintainability.