Post

Created by @nathanedwards
 at November 8th 2023, 5:21:20 am.

Question:

You are given a simple Java program that models a library system. The program contains two classes: Item and Book. The Item class represents any item in the library, while the Book class represents a book with additional attributes.

Complete the code below by implementing inheritance and polymorphism concepts.

public class Item {
    private String title;
    private double price;
    
    // constructor and getters/setters for title and price
    
    // method to print the details of the item
    public void printDetails() {
        System.out.println("Title: " + title);
        System.out.println("Price: $" + price);
    }
}

public class Book extends Item {
    private String author;
    
    // constructor and getter/setter for author
    
    // method to print the details of the book, including author
    // HINT: you can use the 'printDetails()' method of the Item class
    
}

Answer:

public class Item {
    private String title;
    private double price;
    
    public Item(String title, double price) {
        this.title = title;
        this.price = price;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public double getPrice() {
        return price;
    }
    
    public void setPrice(double price) {
        this.price = price;
    }
    
    public void printDetails() {
        System.out.println("Title: " + title);
        System.out.println("Price: $" + price);
    }
}

public class Book extends Item {
    private String author;
    
    public Book(String title, double price, String author) {
        super(title, price);
        this.author = author;
    }
    
    public String getAuthor() {
        return author;
    }
    
    public void setAuthor(String author) {
        this.author = author;
    }
    
    public void printDetails() {
        super.printDetails();
        System.out.println("Author: " + author);
    }
}

Explanation:

  • The Item class has private instance variables title and price, along with their corresponding getters and setters.
  • The Item class also has a constructor that initializes the title and price variables.
  • The Item class has a printDetails() method that prints the title and price of the item.
  • The Book class extends the Item class, inheriting the attributes and methods from it. It also has an additional private instance variable author and its corresponding getter and setter.
  • The Book class has a constructor that calls the super keyword to call the Item class' constructor and initializes the author variable.
  • The Book class also overrides the printDetails() method from the Item class to include the author information. It first calls the super class' printDetails() method to print the inherited details, and then adds the author information.

This code demonstrates inheritance and polymorphism. The Book class extends the Item class, inheriting its attributes and methods. The printDetails() method is polymorphic, as the Book class overrides it to include additional information specific to books.