Disclaimer: This is not an intricate problem. Don't mind the wall of text if you dont want to/have the time to read everything.
I perfectly understand the principle of inheritance, I am just not sure if this is how I am supposed to use it.
I am working on a project where I have a superclass called Item, and to this superclass I will have several subclasses. As for now, it seems like the subclasses will be Book, Film and CD. The program is going to be a lending/borrowing register that could be used in libraries etc.
I've pushed the common attributes for Books, CDs and Films as high as possible in the class hierarchy. I've decided to have title, originator, publishing year and status(as in if someone has borrowed it or not) in the superclass Item.
This is the superclass:
public class Item { protected String title; protected String originator; protected boolean status; protected double publicationYear; public Item(String title, String originator, boolean status, double publicationYear){ this.title = title; this.originator = originator; this.status = status; this.publicationYear = publicationYear; } public String getTitle(){ return title; } public void setTitle(String title){ this.title = title; } public String getOriginator(){ return originator; } public void setOriginator(String originator){ this.originator = originator; } public double getPublicationYear(){ return publicationYear; } public void setPublicationYear(double publicationYear){ this.publicationYear = publicationYear; } public boolean getStatus(){ return status; } public void setStatus(boolean status){ this.status = status; } }
And this is the first subclass I've created:
public class Book extends Item { protected int numberOfPages; protected int isbn; protected String publishingHouse; public Book(String title, String originator, boolean status, double publicationYear, int numberOfPages, int isbn,String publishingHouse){ super(title,originator,status,publicationYear); this.numberOfPages = numberOfPages; this.isbn = isbn; this.publishingHouse = publishingHouse; } }
Now, do I have to create methods for get- and setTitle, get- setOriginator etc. in the subclass?
Also, let's say I want to change name of originator to author, how do I do that? Do i then need to override the setOriginator-methods?
PS. general advice is also appreciated, for example if there's something I could do to decrease the amount of code etc.