Question

I have a little problem understanding the Java language

public class PhonebookEntryList extends List<PhonebookEntry>
{
    public PhonebookEntryList(String filename) throws IOException
    {
        //loadListFromFilename(filename);
    }

    public void saveListToFilename(String filename) throws IOException
    {
        //Do something to save it to a file
    }
}

I can't do this, because List is a generic Type (of course). I know what that means, but at the moment I can't think of a Solution for this problem.

Can you help me to solve it? Thanx!

Was it helpful?

Solution

No, your only problem is that you're extending an interface; you must implement it instead.

public class PhonebookEntryList implements List<PhonebookEntry>

should work; or you might prefer to extend a concrete class:

public class PhonebookEntryList extends ArrayList<PhonebookEntry>

or

public class PhonebookEntryList extends LinkedList<PhonebookEntry>

OTHER TIPS

You can't do that because List is an interface. But! You shouldn't extend or implement a List class to make a PhonebookEntryList, it's a design error.

You should do:

public class PhonebookEntryList
{
    private List<PhonebookEntry> entries;

    public PhonebookEntryList(String filename) throws IOException
    {
        //loadListFromFilename(filename);
    }

    public void saveListToFilename(String filename) throws IOException
    {
        //Do something to save it to a file
    }
}

I.e. your PhonebookEntryList should contain a list instead of inheriting it.

List<T> is an interface, not a class, so you can't inherit from it. You can, however, inherit from a generic type, supplying the type argument, if you wish to create, e.g. a collection for a specific type with some behaviour specific only to that type.

Your Problem is that you are trying to extend an interface rather than implement it.

Composition is what you want. Create a class that wrapps a List (or something that iplements that interface)

and add functionality.

Should I mention that List is an Interface and not a Class? Nah. I think you got the point by now.

I would like to point out, however, that it's usually better NOT to embed the persistence mechanism within the list class. There's this thing called the Visitor pattern that works better. By placing the actual persistency code in a seperate class, the overall logical complexity of the app gets reduced (at the expense of an extra class), and your phonebook becomes liberated to be used in places where having dependencies on the persistency mechanism that looked good when you first designed the code don't look so good anymore. For example, if you wanted to make the Phonebook be an item in an ORM-referenced database.

List<T> is an interface.

If you want to extend a class you'll have to choose an implementation (ArrayList<T> maybe): extends ArrayList<PhonebookEntry>

If you want to implement a List change your code to: implements List<PhonebookEntry>

If you look at the JavaDoc for List, you'll see (as others mentioned) that it's an interface, not a class. What you most likely want to do is look on the same JavaDoc page at "All Known Implementing Classes" and you'll see AbstractList. Extend this. Alternatively, extend one of the non-abstract List implementations.

Note: Most of the time when someone starts to extend one of the Java Collection classes, you're going down the wrong route. Usually, it's better to use one of the existing collections in your class and proxy any collections-style requests that you need. Or return an unmodifiable proxy of your collection:

public class MyClass {
  private final List<PhonebookEntry> myList = new LinkedList<PhonebookEntry>();

  public List<PhonebookEntry> getList() {
    return Collections.unmodifiableList(myList);
  }
}

Usually, it's best to extend a class only if you intend to have different behavior than the class you are extending. Inheritance is more brittle than composition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top