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

kris Moore
kris Moore
1,752 Points

load()

The way I understand it:

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

load() calls the first method which calls load(12), 12 being MAX_PEZ and loads the maximum amount into the dispenser.

load(1) calls the second method and loads 1 (in this case) into the dispenser.

Why not rename the first method to something like refill? So it looks like:

public void refill () { 
load(MAX_PEZ); 
}

Is there a benefit to using the same method signature?

1 Answer

I think the object is to teach student the concept of method overloading. The instructor is telling us that you can multiple methods with same name, in same class, as long as each has a different signature as in:

public void load () public void load (int pezAmount)

same methods name, different signatures

Could you have called the 2nd method public void refill () ???

Sure ... nothing wrong with that.

Hope this help.

If this answers your question, please mark the question as answered.