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 trialKyle Bloyd
Courses Plus Student 1,292 PointsWhy do I have to pass "item" in the new addItem method instead of passing "Product item"?
Why do I have to pass "item" in the new addItem method instead of passing "Product item"?
public class Example {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
Product pez = new Product("Cherry PEZ refill (12 pieces)");
cart.addItem(pez, 5);
/* Since a quantity of 1 is such a common argument when adding a product to the cart,
* your fellow developers have asked you to make the following code work, as well as keeping
* the ability to add a product and a quantity.
*/
Product dispenser = new Product("Yoda PEZ dispenser");
/* Uncomment the line following this comment,
after adding a new method using method signatures,
to solve their request in ShoppingCart.java
*/
cart.addItem(dispenser);
}
}
public class ShoppingCart {
public void addItem(Product item, int quantity) {
System.out.printf("Adding %d of %s to the cart.%n", quantity, item.getName());
/* Other code omitted for clarity. Please imagine
lots and lots of code here. Don't repeat it.
*/
}
public void addItem(Product item) {
addItem(item, 1);
}
}
public class Product {
/* Other code omitted for clarity, but you could imagine
it would store price, options like size and color
*/
private String mName;
public Product(String name) {
mName = name;
}
public String getName() {
return mName;
}
}
4 Answers
Christopher Augg
21,223 PointsHello Kyle,
I think I understand what you are asking. Please let me know if I am totally off base though.
You have an overloaded method named addItem with different parameter lists. The first addItem method takes two arguments of type Product and type int. The second addItem method takes only one argument of type product and then calls the first method by passing the item and quantity. By looking at your code, it seems that you are asking why you do not need to specify Product item when calling the following method:
public void addItem(Product item) {
addItem(item, 1); //Why don't I need Product type here??? i.e addItem(Product item, 1);
}
The reason for this stems from the difference in declaring a method and calling a method. When we write a method declaration, we state what type of arguments will be passed into our method if any. The types can be any of Java's 8 primitive data types like byte , short , int , long , char , boolean , float and double or user defined types like Product. On the contrary, when we call a method, we do not have to specify the type because the method's declaration has already specified what types are allowed as arguments to our method. In this instance, when passing an object (i.e. Product item) in Java, we pass a memory reference to our addItem method call and a quantity of 1. This is a memory location of the item object. However, the int is a primitive type and gets passed as an int instead of a memory reference. When the printf method is called in this addItem method we just passed these arguments to, it can access item at this memory location, get the actual number for quantity, and call the item.getName method.
I hope this helps.
Regards,
Chris
Kyle Bloyd
Courses Plus Student 1,292 PointsThank you...I'm still trying to wrap my brain around what you are saying.
Craig Dennis
Treehouse TeacherYou only need to specify the type when you are defining the method, when calling it you don't include the type declaration.
Make sense?
Kyle Bloyd
Courses Plus Student 1,292 PointsOk, thanks
Dennis Van Laarhoven
988 PointsYes makes sense. Well now it does :-)
Kyle Bloyd
Courses Plus Student 1,292 PointsKyle Bloyd
Courses Plus Student 1,292 PointsI meant to say: Why do I have to make the first argument "item" instead of "Product item" when I call the old method inside of the new addItem method?