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
Franklin Mark Aguba
4,082 PointsPractical uses for method signatures work in Java
Hi! I am currently interested on how method signatures really work in Java. All I understand at the moment is that they can use a method with the same name, but takes different parameters.
I'm interested to know some of its practical uses and any further tips on how I can use them.
Thank you so much!
2 Answers
miguelcastro2
Courses Plus Student 6,573 PointsLet's say I had a simple method that said "hello some person's name". It could look like this:
public String greeting(String name) {
return "Hello " + name;
}
Now let's says I wanted to allow my program to change the language of the greeting or how I greet you. I could just create another greeting() method, but this time add a variable that represents my greeting:
public String greeting(String name, String greeting) {
return greeting + " " + name;
}
So now I have two method signatures for the greeting() method. I could just provide it a name or if I want to I could also provide it the greeting. This is a simple example of how you can use method signatures.
Craig Dennis
Treehouse TeacherHere have a look at the List interface javadoc. Scroll down to the methods, look at add there. First one will add an item, the second one will do it at the right time.
There's a couple in there with same names, different implementations!
Let me know if that clears things up, happy to dive deeper if you need it!