015-001-006
Basic Polymorphism
Easy
Problem Description
Basic polymorphism
In this problem, you will create a program that reads animal types from standard input, defines an Animal parent class with Dog and Cat subclasses, uses polymorphism to call each subclass's overridden makeSound() method through Animal type variables, and displays the result to standard output.
Learning Objective: Understand basic polymorphism where parent class type variables handle child class instances
Overview
Create a program that reads animal types from input, builds an Animal array, and confirms overridden methods are called based on the actual type.
Specifications
- Define makeSound() method in Animal class (outputs "Some sound")
- Dog class extends Animal and overrides makeSound() (outputs "Woof!")
- Cat class extends Animal and overrides makeSound() (outputs "Meow!")
- In Main, build an Animal array from input and call makeSound() on each element
Input Format
N
animal_type_1
animal_type_2
...
- Line 1: number of animals N
- Next N lines: type of each animal ("dog" or "cat")
Output Format
(N lines of each animal's makeSound() output)
Input/Output Example
Input:
2
dog
cat
Output:
Woof!
Meow!
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