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

Two years have passed since the last time I encoded Java, so my coding skills are a bit rusty.

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

To do this, I'm trying to create an abstract class where private variables will be of type List<E> , and then create 2 subclasses depending on the type of variable.

The thing is, I don’t know if I am 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 this make sense? What am I doing wrong? Do you have any recommendations?

2) It would be more reasonable for me to have constructors in DBList and call them (with super() ) in subclasses, but I cannot do this because I can’t initialize the variable with new List<E>() .

3) I thought that someday I would make deep copies, and for this I always redefine the clone() method of my classes and code it accordingly. But these classes never had lists, sets or maps on them; they only had strings, ints, floats. How to make deep copies in this situation?

0
source share
1 answer

You do not need subclasses that require LinkedList<UserProfile> and ArrayList<UserProfile> . Working with List<UserProfile> more than excellent, it is recommended (see Effective Java 2nd Edition, paragraph 52: refer to objects by their interfaces).

Keep in mind that LinkedList<E> implements List<E> and ArrayList<E> implements List<E> , so if you take List<E> , you can take both LinkedList<E> and ArrayList<E> tags already (and all other List<E> developers there).


Regarding clone()

It is now well known that clone() deeply broken in Java and should not be used (see Effective Java 2nd Edition, paragraph 11: it is wise to override clone).

From an interview with author Josh Bloch :

If you read the article on cloning in my book, especially if you read between the lines, you will find that I think clone deeply broken [...] There are very few things for which I use Cloneable more [...] This shame that Cloneable broken, but it happens.

Related Questions

+4
source

Source: https://habr.com/ru/post/1310516/


All Articles