016-003-012

Animal Array Polymorphism

Medium

Problem Description

Animal Array Polymorphism

In this problem: You will implement Dog and Cat classes that extend the abstract class Animal, store them polymorphically in an Animal[] array, and display each animal's information to standard output.

Learning Objective: Understand how to use polymorphism with abstract class arrays

Overview

The Animal abstract class has a name field and an abstract method speak(). Dog and Cat return different sounds. Store them in an Animal[] array and loop with a for loop to output each animal's information.

Specifications

  • Animal abstract class: name field, constructor, getName() method, abstract method speak()
  • Dog: speak() returns "Woof!"
  • Cat: speak() returns "Meow!"
  • Store Dog("Pochi"), Cat("Tama"), Dog("Hachi") in an Animal[] array
  • Output each element in the format "{name}: {speak()}"

Output Format

Pochi: Woof!
Tama: Meow!
Hachi: Woof!

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