Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lot of good answers, but maybe one more from a self-taught Java programmer as I went through all that by myself with a lot of pain ;)</p> <p><strong>Think about a Class as something seen from the outside, not as something you see internally</strong>. If you look at a Class from the outside, what you see?</p> <p>Taking the clock as an example again, a clock can <em>give you info about the current time</em> and it can be <em>set up to show the right time</em>.</p> <p>So looking at things from the outside, a clock is a machine that can do those two things; <em>public methods we call them</em>. </p> <p>But we as constructors of this clock we know that before any time operation we have to switch from 23 to 11 on our display (it's that kind of clock), so we have to rearrange things internally a bit to do so. Changing from 23 to 11 works just fine for us in both cases - setting the clock and showing the current time - but we do it <em>"on the side"</em> as the user doesn't have to know about all that complicated math. These are private methods!</p> <p>So our <code>Clock</code> Class could have two <strong>public methods</strong> (<code>showTime</code> and <code>setTime</code>) which are all that the user <strong><em>wants</em></strong> to see, and a private method (<code>recountTime</code>) that provides functionality for these <strong>public methods</strong> and are something that the user <strong><em>doesn't want</em></strong> to see. </p> <p>So on the one hand, you should keep in mind that private is what <strong>won't be reimplemented and accessed by future programmers</strong> using your code (as was pointed at in the answers above). But private also means <strong>things done on the side, so the user don't see it</strong>. That's why we call the public methods a <strong>public interface</strong> - it's all the user will see from the outside. </p> <p>For me it is helpful (I'm self-taught, so maybe it's not a very popular methodology...) to write down everything the users (real users and other Classes) will do with my Class (public interface with just public methods' signatures), then to write the signatures of private methods that <strong>I-the-implementer</strong> will use to accomplish the public goals that promised to provide to my users and then just fulfill it with code. </p> <p>It can be helpful to keep in mind that the old C rule is still valid (as was expressed in <a href="http://programmer.97things.oreilly.com/wiki/index.php/97_Things_Every_Programmer_Should_Know" rel="noreferrer">97 Things Every Programmer Should Know</a>): a function/method should be just a few lines long, really!! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload