File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes extending an arraylist\hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending an arraylist\hashmap" Watch "extending an arraylist\hashmap" New topic
Author

extending an arraylist\hashmap

leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
hi all!

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).
thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to JavaRanch

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>.
Anjali Raman
Ranch Hand

Joined: Nov 28, 2007
Posts: 57
Hi Leroy,
You dont have to extend it with the ArrayList class instead just import the java.util.ArrayList;
Its not a good practise to extend the standard class instead just import them.

Anjali
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Thank you both for the fast replays!
so to really understand what you say ill give another question..
i have a public abstract class

it takes the data from my dataReader class, and converts each "String[] line" variable into the relevant type of data.

so again, for this example i should change the" extends hashmap"? and create a hashMap object inside the class?

Thanks again!
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
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.
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
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?
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
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.
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
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.
Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extending an arraylist\hashmap
 
Similar Threads
Passing Jave object arrays
Collections.shuffle problem
ArrayOutOf Bound Exception
Help with ArrayList error
OO question: Interface or abstract class.