Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate subclass given superclass
    primarykey
    data
    text
    <h2>Problem Description</h2> <p>I have an abstract <code>Paper</code> class that contains common properties of all papers and one or more child classes of paper that add additional information for that type of paper. I then have a <code>HashMap&lt;String, Paper&gt;</code> to store multiple papers.</p> <p>My application allows the user to update a paper by providing a the <code>pid</code> and then supplying the attributes and values to update. The issue I am having is how do I update the properties on the sub classes when all I have is the super class.</p> <p><strong>What is the best way/practice to handle this situation?</strong></p> <h2>Class Structure</h2> <pre><code>public abstract class Paper { String pid; String title; String author; } public class Publication extends Paper { int pages; } public class PHDThesis extends Paper { String supervisor; } </code></pre> <hr> <h2>My Current Attempt</h2> <p>This is what I currently have** and it works by using <code>instance of</code>; but I feel there should be a better way to do this.</p> <pre><code>import java.util.*; public class App { public static abstract class Paper { private String title; private String author; public Paper(String title, String author) { this.title = title; this.author = author; } public void update(String title, String author) { this.title = title; this.author = author; } } public static class Publication extends Paper { private int pages; public Publication(int pages, String title, String author) { super(title, author); this.pages = pages; } public void update(String title, String author, int pages) { super.update(title, author); this.pages = pages; } } public static class PHDThesis extends Paper { private String supervisor; public PHDThesis(String supervisor, String title, String author) { super(title, author); this.supervisor = supervisor; } public void update(String title, String author, String supervisor) { super.update(title, author); this.supervisor = supervisor; } } public static void main(String[] args) { HashMap&lt;String, Paper&gt; papers = new HashMap&lt;String, Paper&gt;(); papers.put("P001", new PHDThesis("My Super", "My PHD Title", "My Author")); papers.put("P002", new Publication(22, "My Pub Title", "My Author")); Paper p = papers.get("P001"); if (p instanceof PHDThesis) { ((PHDThesis)p).update("New Title", "New author", "New Super"); } else if (p instanceof Publication) { ((Publication)p).update("New Title", "New author", 33); } } } </code></pre> <p>** reduced test code, actual code is much more complex and better laid out.</p>
    singulars
    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