Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial<noob />
17,062 PointsWhy we are making the method normalizedGuess private?
Hi, Why we are actually making normalizedGuess private? why not public? for my understandings and as Craig said>> we making it private to prevent it from being called in the other classes
but why we need to prevent it?
2 Answers
Eric M
11,546 PointsHi noob,
In Java it is convention to keep things private if they don't need to be directly accessed, as well as to create ways of accessing things that are consistent and controlled.
normalizeGuess is not particularly useful as a direct call outside of the game class, especially as it throws exceptions based on the hits or misses of in the instantiated Game object.
Put another way, normalizeGuess is just a helper method for the public applyGuess method. applyGuess is how we want people to check guesses for the game object, and during an application of a guess is the only time we (currently) expect normalizeGuess to be called. To prevent confusion we make the number of possible interactions as few as possible to prevent programmers from incorrectly calling normalizeGuess then passing the result to applyGuess, which would cause normalizeGuess to run twice. Not a problem in this case, but if normalizeGuess changed any state or had any other side effects, we may want to control how often they trigger.
In short, as we don't expect normalizeGuess to be used outside of other methods in the Game class, it should be private.
Cheers,
Eric
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsEffectively, when thinking in an object-orientated way, you need to think do you need this method to be accessed outside of the class ? If you think you will use the method outside the class then you should make it public otherwise make it private. Making it private prevents any unnecessary changes to the object.
For your case, you have no need to normalize the guess outside of the class as that's used internally to validate the input and to give reusability to the object.
Hope this helps
<noob />
17,062 PointsHi, thanks for the explantion i understand why we use it in that way. I have another question, i saw your a techdegree java student u enjoy the courses so far?
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsGood you understand now. Yeah, the Java tech-degree has been really good. Would recommend if you want to learn further than the traditional content provided
<noob />
17,062 Points<noob />
17,062 PointsThanks for ur clear explantion i got it now :D
Eric M
11,546 PointsEric M
11,546 PointsNo worries at all, there's a lot of information in these courses!