Java and AbstractList

Discussion in 'Mac Programming' started by Buschmaster, Dec 2, 2007.

  1. Buschmaster macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #1
    So, I'm quite confused by AbstractList and I'm working on an assignment for a class. Basically what we're doing is creating a catalog for a library where it will store the information such as title, ISBN, etc.

    I am getting all the information in correctly and creating objects in the appropriate classes, but I don't know how to work AbstractList very well. Here's what I have so far...

    BookList.java
    [CODE ]import java.util.*;
    import java.io.*;
    import containers.AbstractList;

    public abstract class BookList<Element> extends AbstractList<Object> {
    public int size () {
    return this.size();
    }

    }[/CODE]

    And this is inside ListEngine.java
    Code:
    		ListEngine engine = new ListEngine();
    		BookList blist = new BookList<ListEngine>();
    My problem is coming from:
    Code:
    BookList blist = new BookList<ListEngine>();
    Here is what Eclipse is telling me:
    Thanks for any help provided! :)
     
  2. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #2
    If a class is declared 'abstract', as you did for BookList, that means you can't create an object of that type. By declaring something abstract, you're saying "This is not meant to be instantiated directly; rather you should extend this first and use that instead."

    For example, think of an abstract class called Shape, which has a method (which should be abstract as well) called getArea(). Depending on the actual shape you care about, like a Circle or Rectangle for example, you'd need to implement getArea() differently, right? Calling Shape.getArea() directly would make no sense because a Shape is an abstract idea. You need a concrete implementation for it to make sense.

    In your case, I am guessing that you don't actually want to make BookList abstract. Also, the error:

    - BookList is a raw type. References to generic type BookList<Element> should be parameterized

    probably means you need to make the line read like this:
    Code:
    BookList<ListEngine> blist = new BookList<ListEngine>();
    
     
  3. aLoC macrumors 6502a

    Joined:
    Nov 10, 2006
    #3
    You probably don't want a class called BookList unless the assignment explicitly tells you to. Just use an ordinary list and parameterize it with your Book class, e.g.

    Code:
    class Book {
      String title;
      String isbn;
      String author;
      // ...
    }
    
    List<Book> catalog = new ArrayList<Book>();
    
     

Share This Page