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.
I teach computer science to undergrads and write for The Renegade Coder. I'm most likely taking care of my daughter, watching the Penguins, or reading manga.
Location
Columbus, Ohio
Education
B.S. in CE from CWRU 2016; M.S. in CSE from OSU 2020; PhD in EED from OSU 2024
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.
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.