015-001-007

Polymorphism in Method Parameters

Hard

Problem Description

Polymorphism in Method Parameters

In this problem, you will create a program that defines a print(Document doc) method in the Printer class accepting a parent class type parameter, reads document types from standard input, and creates appropriate child class instances (PDF, WordDocument) to demonstrate polymorphism.

Learning Objective: Understand how using parent class type in method parameters provides flexibility

Overview

Define a method with parent class type parameter, read document types from input, and confirm different child class instances can be passed to the same method.

Specifications

  • Define print(Document doc) method in Printer class
  • Define getContent() method in Document class (returns "Generic document")
  • PDF and WordDocument classes extend Document and override getContent()
  • Use Scanner to read N (document count), then N lines of document types ("PDF" or "Word")
  • Create the appropriate instance for each type and print via Printer

Input Format

N
type1
type2
...
  • Line 1: number of documents N
  • Subsequent lines: document type ("PDF" or "Word") for each

Output Format

Printing: PDF content
Printing: Word content

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up