015-001-007

Polymorphism in Method Parameters

Hard

Problem Description

Polymorphism in Method Parameters

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

Overview

Define a method with parent class type parameter and confirm different child class instances can be passed.

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()
  • Confirm Printer can print any type of Document

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?