Help with abstract class in Java with private variable of type List<E>

Posted by Nazgulled on Stack Overflow See other posts from Stack Overflow or by Nazgulled
Published on 2010-05-21T11:42:22Z Indexed on 2010/05/21 11:50 UTC
Read the original article Hit count: 198

Hi,

It's been two years since I last coded something in Java so my coding skills are bit rusty.

I need to save data (an user profile) in different data structures, ArrayList and LinkedList, and they both come from List. I want to avoid code duplication where I can and I also want to follow good Java practices.

For that, I'm trying to create an abstract class where the private variables will be of type List<E> and then create 2 sub-classes depending on the type of variable.

Thing is, I don't know if I'm doing this correctly, you can take a look at my code:

Class: DBList

import java.util.List;

public abstract class DBList {

    private List<UserProfile> listName;
    private List<UserProfile> listSSN;

    public List<UserProfile> getListName() {
    return this.listName;
    }

    public List<UserProfile> getListSSN() {
    return this.listSSN;
    }

    public void setListName(List<UserProfile> listName) {
    this.listName = listName;
    }

    public void setListSSN(List<UserProfile> listSSN) {
    this.listSSN = listSSN;
    }

}

Class: DBListArray

import java.util.ArrayList;

public class DBListArray extends DBList {

    public DBListArray() {
    super.setListName(new ArrayList<UserProfile>());
    super.setListSSN(new ArrayList<UserProfile>());
    }

    public DBListArray(ArrayList<UserProfile> listName, ArrayList<UserProfile> listSSN) {
    super.setListName(listName);
    super.setListSSN(listSSN);
    }

    public DBListArray(DBListArray dbListArray) {
    super.setListName(dbListArray.getListName());
    super.setListSSN(dbListArray.getListSSN());
    }

}

Class: DBListLinked

import java.util.LinkedList;

public class DBListLinked extends DBList {

    public DBListLinked() {
    super.setListName(new LinkedList<UserProfile>());
    super.setListSSN(new LinkedList<UserProfile>());
    }

    public DBListLinked(LinkedList<UserProfile> listName, LinkedList<UserProfile> listSSN) {
    super.setListName(listName);
    super.setListSSN(listSSN);
    }

    public DBListLinked(DBListLinked dbListLinked) {
    super.setListName(dbListLinked.getListName());
    super.setListSSN(dbListLinked.getListSSN());
    }

}

1) Does any of this make any sense? What am I doing wrong? Do you have any recommendations?

2) It would make more sense for me to have the constructors in DBList and calling them (with super()) in the subclasses but I can't do that because I can't initialize a variable with new List<E>().

3) I was thought to do deep copies whenever possible and for that I always override the clone() method of my classes and code it accordingly. But those classes never had any lists, sets or maps on them, they only had strings, ints, floats. How do I do deep copies in this situation?

© Stack Overflow or respective owner

Related posts about java

Related posts about abstract-class