Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with abstract class in Java with private variable of type List<E>
    primarykey
    data
    text
    <p>It's been two years since I last coded something in Java so my coding skills are bit rusty.</p> <p>I need to save data (an user profile) in different data structures, <code>ArrayList</code> and <code>LinkedList</code>, and they both come from <code>List</code>. I want to avoid code duplication where I can and I also want to follow good Java practices.</p> <p>For that, I'm trying to create an abstract class where the private variables will be of type <code>List&lt;E&gt;</code> and then create 2 sub-classes depending on the type of variable.</p> <p>Thing is, I don't know if I'm doing this correctly, you can take a look at my code:</p> <p><strong>Class: DBList</strong></p> <pre><code>import java.util.List; public abstract class DBList { private List&lt;UserProfile&gt; listName; private List&lt;UserProfile&gt; listSSN; public List&lt;UserProfile&gt; getListName() { return this.listName; } public List&lt;UserProfile&gt; getListSSN() { return this.listSSN; } public void setListName(List&lt;UserProfile&gt; listName) { this.listName = listName; } public void setListSSN(List&lt;UserProfile&gt; listSSN) { this.listSSN = listSSN; } } </code></pre> <p><strong>Class: DBListArray</strong></p> <pre><code>import java.util.ArrayList; public class DBListArray extends DBList { public DBListArray() { super.setListName(new ArrayList&lt;UserProfile&gt;()); super.setListSSN(new ArrayList&lt;UserProfile&gt;()); } public DBListArray(ArrayList&lt;UserProfile&gt; listName, ArrayList&lt;UserProfile&gt; listSSN) { super.setListName(listName); super.setListSSN(listSSN); } public DBListArray(DBListArray dbListArray) { super.setListName(dbListArray.getListName()); super.setListSSN(dbListArray.getListSSN()); } } </code></pre> <p><strong>Class: DBListLinked</strong></p> <pre><code>import java.util.LinkedList; public class DBListLinked extends DBList { public DBListLinked() { super.setListName(new LinkedList&lt;UserProfile&gt;()); super.setListSSN(new LinkedList&lt;UserProfile&gt;()); } public DBListLinked(LinkedList&lt;UserProfile&gt; listName, LinkedList&lt;UserProfile&gt; listSSN) { super.setListName(listName); super.setListSSN(listSSN); } public DBListLinked(DBListLinked dbListLinked) { super.setListName(dbListLinked.getListName()); super.setListSSN(dbListLinked.getListSSN()); } } </code></pre> <p><strong>1)</strong> Does any of this make any sense? What am I doing wrong? Do you have any recommendations?</p> <p><strong>2)</strong> It would make more sense for me to have the constructors in <code>DBList</code> and calling them (with <code>super()</code>) in the subclasses but I can't do that because I can't initialize a variable with <code>new List&lt;E&gt;()</code>.</p> <p><strong>3)</strong> I was thought to do deep copies whenever possible and for that I always override the <code>clone()</code> 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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload