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

Java

I didn't understand the fill(MAX_PEZ); thing we are doing in this video

Can you please explain what we are doing with the fill(MAX_PEZ); in this video (https://teamtreehouse.com/library/method-overloading)

1 Answer

You have two fill methods:

  • with fill(int pezAmount) you can fill the number of pez you want, you just pass the amount you want as a parameter
  • with fill() you can fill a fixed number of pez, namely the amount MAX_PEZ

But they both add pez. So both methods have the same logic. The only difference is the amount: one uses a variable to find out how many pez to add, and the other has a preset amount MAX_PEZ instead. The fill() method is like a special case of the fill(int pezAmount) method, where pezAmount is equal to MAX_PEZ.

So why make two separate implementations of the same logic? Instead you implement the logic of how to add pez once, in the more generic method fill(int pezAmount). Then you add another method fill(), and inside that method, you call(int pezAmount) and pass it the fixed value MAX_PEZ as a parameter. That way, if you need to do some changes to the logic of adding pez later, you only need to do it in the method fill(int pezAmount).

Was that helpful? If you have follow up questions, please ask.

Thanks a lot.