Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.