• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

extending an arraylist\hashmap

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic