Courseiva
Object-Oriented ProgrammingmediumMultiple ChoiceObjective-mapped

1Z0-811 Object-Oriented Programming Practice Question

A development team is building a library management system. The system has classes 'LibraryItem', 'Book', and 'DVD'. LibraryItem has a method 'getTitle()' that returns the title. Book and DVD extend LibraryItem. The team wants to ensure that when a LibraryItem is borrowed, a message specific to its type is displayed. They have a 'Borrower' class with a method 'borrow(LibraryItem item)', which currently calls 'item.getTitle()' and prints the title. Now they need to display 'Book borrowed' or 'DVD borrowed' based on the actual item type. They want to avoid using 'instanceof' checks in the 'borrow' method to keep it open for new item types. Which design should they use?

⚠ Common exam trap

Oracle often tests the Open/Closed Principle by presenting options that appear to work (like a type field or switch on class name) but violate extensibility, tempting candidates to choose a quick fix instead of the polymorphic solution.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Define an abstract method 'getBorrowMessage()' in LibraryItem, and implement it in Book and DVD. Call that method in borrow().

It applies the Open/Closed Principle: LibraryItem defines an abstract getBorrowMessage() method, and each subclass provides its own implementation. The borrow() method calls this polymorphic method without needing instanceof or type checks, so adding new item types (e.g., Magazine) only requires implementing getBorrowMessage() in the new subclass without modifying existing code.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Add a String field 'type' to LibraryItem and set it in each subclass constructor. In borrow(), check the field.

    Why it's wrong here

    Still requires conditional logic, not extensible for new types without modifying borrow().

  • Use a switch statement on the class name in borrow().

    Why it's wrong here

    Requires modification when new types added; not open for extension.

  • Define an abstract method 'getBorrowMessage()' in LibraryItem, and implement it in Book and DVD. Call that method in borrow().

    Why this is correct

    Polymorphism allows new subclasses to define their own message without changing borrow().

  • Override the 'borrow' method in each subclass and call a different method.

    Why it's wrong here

    Overriding borrow in subclasses would duplicate borrowing logic; not clean.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.