I am learning Java and followed a tutorial like this:
package segovia.java.learn;
import java.util.ArrayList;
public class GroceryList {
private ArrayList<String> groceryList = new ArrayList<String>();
public void addGroceryItem(String item) {
groceryList.add(item);
}
public void printGroceryList() {
System.out.println("You have " groceryList.size() " items in your grocery list.");
for (int i=0; i < groceryList.size(); i ) {
System.out.println((i 1) ". " groceryList.get(i));
}
}
public int getLength(){
return groceryList.size();
}
}
package segovia.java.learn;
public class Main {
public static void main(String[] args) {
GroceryList myGroceries = new GroceryList();
myGroceries.addGroceryItem("apple");
myGroceries.addGroceryItem("orange");
myGroceries.addGroceryItem("pork");
myGroceries.addGroceryItem("beef");
myGroceries.printGroceryList();
System.out.println(myGroceries.getLength());
}
}
which worked fine. However, I thought that since the myGroceries
is an ArrayList, so it should have the .size()
method. So I added a line of
System.out.println(myGroceries.size());
but IntelliJ told me it's not working. So I thought that MAYBE I should use inheritance so I changed to
public class GroceryList extends ArrayList {
now the .size()
method is working, but it gave me a wrong value.
Final codes are like this:
package segovia.java.learn;
import java.util.ArrayList;
public class GroceryList extends ArrayList{
private ArrayList<String> groceryList = new ArrayList<String>();
public void addGroceryItem(String item) {
groceryList.add(item);
}
public void printGroceryList() {
System.out.println("You have " groceryList.size() " items in your grocery list.");
for (int i=0; i < groceryList.size(); i ) {
System.out.println((i 1) ". " groceryList.get(i));
}
}
public int getLength(){
return groceryList.size();
}
}
package segovia.java.learn;
public class Main {
public static void main(String[] args) {
GroceryList myGroceries = new GroceryList();
myGroceries.addGroceryItem("apple");
myGroceries.addGroceryItem("orange");
myGroceries.addGroceryItem("pork");
myGroceries.addGroceryItem("beef");
myGroceries.printGroceryList();
System.out.println(myGroceries.getLength());
System.out.println(myGroceries.size());
}
}
and the getLenth()
gave me 4, which is correct, while the .size()
gave me 0, which is obviously wrong.
Can anyone help me understand why the codes work like this? I guess it's related to the class inheritance but not sure.
CodePudding user response:
I think there is a misunderstanding of inheritance vs composition. In this case, before you added extends ArrayList
to GroceryList
, your GroceryList
has a list. That's why .size()
wasn't working on it, because the GroceryList
you defined doesn't have a size
method. It does have a list called groceryList
though. That's why after you addGroceryItem
, the getLength
method works correctly. It's getting the size of groceryList
.
After you added extends ArrayList
, GroceryList
now is an ArrayList
, that's why it has a size
method now. But it is not the same list as groceryList
. Hence getting the size of myGroceries
, after addGroceryItem
doesn't give you the size you expected. Because you never added anything to the ArrayList
that is myGroceries
. You've only added thing to the groceryList
that is a field in myGroceries
.