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 Java Objects (Retired) Harnessing the Power of Objects Method Signatures

hakan Gülay
hakan Gülay
8,489 Points

load();

İn this code, Could u expalin this code step by step ı am really confused .How it pasted 6 chomp! ? you can understand what ı mean when you watch video

dispenser.load(4);
dispenser.load(2);


public void load() {
    load(MAX_PEZ); 
  }
  public void load (int pezAmount)
  {
    mPezCount += pezAmount;
  }

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

HI hakan,

the code above shows the principle of method overloading. So you can have multiple methods with the same name ("load" in our example) but every method has different arguments. Or sometimes you will see the word parameters.

In your example you have a load() method with empty parenthesis (). So this method takes no arguments. The second load method has int pezAmount inside (). So it takes a number (int) of Pez that need to be filled in the PezDispenser.

This load method looks like this:

// so if you call this load method you need to add a number of Pez 
// into the parenthesis that you want to fill into PezDispenser
  public void load (int pezAmount)
  {
// this number is pezAmount and will be added/loaded to mPezCount
    mPezCount += pezAmount;
  }

The first two lines of code show how you use the load(int pezAmount):

dispenser.load(4);
dispenser.load(2);

Here you are using load(int pezAmount) directly an you give the method 4 Pez and then 2 Pez to load. So 4 and 2 are numbers and they are arguments that your load(int pezAmount) needs to work to load the PezDispenser called "dispenser"

The load method that takes no arguments has your second load(int pezAmount) inside. This might seem a little confusing:

public void load() {
// so if you call dispenser.load(); with empty perenthesis
// the PezDispenser will be loaded to MAX-PAZ
// and uses the second load method that takes the number of Pez (here MAX-PEZ)
    load(MAX_PEZ); 
  }

I hope my explanations can help you a little bit ...

Grigorij

hakan Gülay
hakan Gülay
8,489 Points

Thanks so much Grigorij!,I got it know thanks again :) ı was confused but now ı got all of them clearly :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey hakan, you are welcome :)

Stay tuned !!!