i want to have a class that reads a file and get the data as an ArrayList<String> (each Strin in the arrylist is a line from the file).
should i create the new class as:
or should i use the arraylist as a variable?
or it isn't matter?
is extending ArrayList is something acceptable?
thanks all, and sorry for this beginner question..
i usually find myself creating classes that extending Arraylist or Maps, and i wanted to know if there are any drawbacks, or problems with this way of working.
the propose of this Datareader class is only to read a given file and store the data for later use - in an ArrayList.
(i use the data i read later in another class).
Yes, it does matter; there is a big design difference between the two possibilities.
Only ever say "extends" when you can truthfully say "this class IS-A that class." Your DataReader is not an ArrayList, but it uses an ArrayList to store the data. So you should always use the version with the List as a field.
By they way: It should read
List<String> list = new ArrayList<String>();
By using the interface (List) you allow for more flexibility of use. Similarly, your methods should return List<String> rather than ArrayList<String>.
It's a bit of a quandary when to extend or "wrap" something like a List. I used to always wrap them (i.e. I used an instance field to store the list, and coded my own get or add methods to add to that hidden list). An example might be a "Classroom" that that would have methods for adding or removing students. It's "list like", because in one regard it's a collection of Student objects, but not a list in the Java collections sense because not all methods in those interfaces are applicable. Then Java introduced generics, which made it more tempting to subclass, since the "add" and "get" methods would automatically be typed. However, as Campbell pointed out, you really need to consider how you want other people to use the class. If it's fundamentally a List, then go ahead and subclass. If the list is just one aspect of the class, then use a field. Maybe it boils down to the OO design rule of thumb "favor composition over inheritance."
Joined: Oct 13, 2005
Agree about "favour composition over inheritance." A classroom IS-NOT-A list of children. It is a place where children learn. You can list those children in a list, so it is appropriate for the classroom to have a list of children in it. In fact when I was in school, teacher would read out the list of children for that classroom and we had to call out "present, Miss" to confirm we were there.
If your classroom class extended a List, then it would be possible to add children, remove children or count children without going through the classroom or teacher. So it is not appropriate for the Classroom to extend a List<Child>.
Similarly I would think your CodedDataBase class might have a Map as a field, rather than extending a Map.
Joined: Sep 24, 2009
i understand what you say and it is all making sense, but lets look again at the dataReader class.
the only variable there is the List<String>. so actually, i use the list, its methods(add,get) and its storage capability, and the only thing i change, is adding a method - loadFile(String filename) - to fill this list.
it is like having a Deck class. a Deck is only a set of Card objects.
anyway, is that wrong to do so? or is it a "design" problem i should avoid?
Joined: Nov 06, 2000
Just keep in mind how the calling routine can use a Set. Does it make sense to removeAll() and retainAll()? Etc. Will the class need other features that have nothing to do with Set? You can only extend a single class, but with composition you can add as many features as you want.
For your example, yeah, maybe a Deck really is just a Set of Card.
And by the way, Apache Commons has method org.apache.commons.io.IOUtils.readLines() that returns a List of String.
Joined: Oct 13, 2005
You could argue that there is another way to design your Reader class. Don't give it any fields, but simply static methods. You can have a readFileIntoStringList method which takes a file and a List<String> as parameters, and fills the List with the contents of the file.
Joined: Sep 24, 2009
Thank you all, i now really understand when to extend a class!
Cambell, what you said about the static method, it seems like a good way and i implemented it.