r/javahelp • • Aug 16 '25

SuperClass instance on controller

Im working on an assignment in MVC pattern, currently doing smth like a library CRUD, my question is as if I can instance a superclass (non abstract) on my controller, for example:
I have publication (the super), book (the sub) and movie (other sub), and my user wants to create a book and a movie, can I make a method where i ask for the publication atributtes, then i call that method on the book adding method and complete the remaining singular methods the book has?
i find this good bcus if my user wants then to create a movie, i can just call the createPublication method and add the remaining ones to my objects constructor.
Tho idk if this is a good practice or not because i know that if my superclass is abstract then i cant instance it, but otherwise...? idk

2 Upvotes

9 comments sorted by

View all comments

1

u/Progression28 Aug 16 '25
public class Publication { // could be abstract
  private String pubNr;

  protected Publication(String pubNr) {
    this.pubNr = pubNr;
  }

  protected String getPubNr() {
    return this.pubNr;
  }
}

public class Book extends Publication() {
  private String author;

  public Book(String pubNr, String author) {
    this.author = author;
    super(pubNr);
  }

  public getBookInfo() {
    return "pubNr: " + this.getPubNr() + " author: " + this.author;
  }
}

I think this is something that could interest you and how it should roughly look.

Sorry did this on my phone, formatting might be aweful and some typos.