Skip to content
ExamSpine

Answer it

P2 b134 library reference book

5 marksYou mark it yourself against the marking points

The school library's Book class has a constructor, an accessor get_available (in Java, getAvailable) and a method borrow that takes one copy if one is available and returns whether it did. Reference books, such as atlases, must stay in the library, so a programmer adds this subclass. Here it is in Python and in Java.

class ReferenceBook(Book):
    def __init__(self, title, copies, room):
        super().__init__(title, copies)
        self.room = room

    def borrow(self):
        return False

shelf = [Book("Dune", 1),
         ReferenceBook("Atlas", 3, "R2")]
for b in shelf:
    print(b.borrow())
public class ReferenceBook extends Book {
    private String room;

    public ReferenceBook(String title,
            int copies, String room) {
        super(title, copies);
        this.room = room;
    }

    @Override
    public boolean borrow() {
        return false;
    }
}

// in main
ArrayList<Book> shelf = new ArrayList<>();
shelf.add(new Book("Dune", 1));
shelf.add(new ReferenceBook("Atlas", 3, "R2"));
for (Book b : shelf) {
    System.out.println(b.borrow());
}

(a) Outline what ReferenceBook inherits from Book, and what it adds. [2]

(b) The loop prints True then False (in Java, true then false). Explain how polymorphism produces this output, although the loop makes the same call each time. [3]

[5]