Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A question about inherited methods

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Thanked 0 Times in 0 Posts

    Default A question about inherited methods

    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){
    		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.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: A question about inherited methods

    No, you don't have to define them in your sub-classes unless they were declared abstract to begin with (of course, you can then declare your sub-class to be abstract and not have to worry about them, but that's a whole different issue). Also, they will inherit any functionality their super class had until they are over-ridden. All the methods and fields are inherited, so no, you don't have to create your own setters in the sub-class (unless you want different functionality).

    I don't see too many ways to decrease the amount of code, it actually looks quite nice. One thing I would do though, is add comments and javadoc. They will make your life a little harder right now, but in the future they will make using/debugging this code so much easier.

Similar Threads

  1. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  2. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  3. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM
  4. Novice with java methods, please help!
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2009, 04:23 AM
  5. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM