DEV Community

Cover image for The Difference Between Public and Private in Java

The Difference Between Public and Private in Java

Jeremy Grifski on April 11, 2019

As I was writing my first semester of teaching reflection, I got the idea to kick off a series of student questions called Coding Tangents. In this...
Collapse
 
devworkssimone profile image
DevWorksSimone • Edited

Hi there...
Thanks for sharing this. I always like to find material on Basic subject that May help even ppl who dont program for work like me. I think that I grasped what is the use. Stille have 2 question: first is not really related to this subject but i Am not plain satisfied If i dont understand every little piece of code i read... how the pubblic method is supposed to know the lenght of the wipers array if we didnt set any? Am i losing something? Second question, why would i As a developer wanted this to be hidden in the code if only other developer are going to look at the code ? I mean final user Will Never see the code he will just click on a button or perform An action that will trigger my function/class... sorry If these are dumb questions but they really come from A layman.

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.

Collapse
 
lluismf profile image
Lluís Josep Martínez

The HellowWorld example is not the best fit to show how public and private work. It's much more clear with typical data structures like Stack, List etc. or with domain entities like Order, Invoice etc.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

I appreciate the feedback, but I’m not sure I agree (at least with the first part). If we’re talking beginners, they aren’t going to be able to handle data structures beyond arrays (maybe ArrayLists, LinkedLists)—especially data structures in java which leverage generic types. If they knew those things already, then they probably wouldn’t need this article.

As for domain entities, to be quite honest, I’ve never heard that term. Granted, if we’re just talking about user-defined classes, then that’s fair. I just felt like it was important to come full circle on the example at the beginning of the article. When I get the chance, I’ll extend the last section to include a better example. Thanks!