Help with abstract class in Java with private variable of type List<E>
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>());
}
publi开发者_开发技巧c 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?
You shouldn't need subclasses that takes LinkedList<UserProfile>
and ArrayList<UserProfile>
to begin with. Working with List<UserProfile>
is more than fine, it's recommended (see Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces).
Do keep in mind that LinkedList<E> implements List<E>
, and ArrayList<E> implements List<E>
, so if you take a List<E>
, you can take both LinkedList<E>
and ArrayList<E>
already (and all other implementors of List<E>
out there).
Regarding clone()
It's well understood now that clone()
is deeply broken in Java, and should not be used (see Effective Java 2nd Edition, Item 11: Override clone judiciously).
From an interview with author Josh Bloch:
If you've read the item about cloning in my book, especially if you read between the lines, you will know that I think
clone
is deeply broken [...] There are very few things for which I useCloneable
anymore [...] It's a shame thatCloneable
is broken, but it happens.
Related questions
- Deep clone utility recomendation recomendation
- Java: how to clone ArrayList but also clone its items?
- Why people are so afraid of using clone() (on collection and JDK classes) ?
- How to properly override clone method?
- clone() vs copy constructor vs factory method??
精彩评论