014-002-006

Method Override: Animal Sounds

Medium

Problem Description

Method Override: Animal Sounds

In this problem, you will create a program that overrides the makeSound() method in Cat and Dog classes that extend a parent Animal class, and outputs the appropriate sound for each animal type read from standard input.

Learning Objective: Understand how to change parent class behavior using method overriding

Overview

Method overriding allows redefining parent class methods in child classes. This enables different behaviors with the same method name.

Specifications

Create parent class Animal:

  • makeSound() method: outputs "Some sound"

Create child class Cat:

  • Extends Animal
  • Override makeSound(): outputs "Meow"

Create child class Dog:

  • Extends Animal
  • Override makeSound(): outputs "Woof"

In Main class:

  • Read the number of animals n from standard input
  • Read n lines, each containing an animal type ("cat", "dog", or "animal")
  • Create an instance of the appropriate class and call makeSound() for each

Input Format

n
type1
type2
...
  • Line 1: number of animals n (n ≥ 1)
  • Next n lines: animal type, one of cat, dog, or animal

Output Format

Output each animal's sound on a separate line in input order.

(sound of type1)
(sound of type2)

Sample I/O

Input:

2
cat
dog

Output:

Meow
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