014-001-010

Method Overriding

Medium

Problem Description

method Overriding

In this problem, you will create a program that inherits the Animal class in a Cat class, overrides the speak() method to produce different output, and displays the result to standard output.

Learning Objective: Understand how to override parent class methods in child classes

Overview

Overriding is redefining a parent class method in a child class. You can implement different behavior with the same method name, parameters, and return type.

Input

Read one line from standard input: the cat's name.

name

Specifications

  1. Define a speak() method in the Animal class that prints "Animal speaks"
  2. Create the Cat class by inheriting from the Animal class
  3. The Cat class takes a name (String) in its constructor and stores it in a field
  4. override the speak() method in the Cat class to output "Meow! I'm [name]"
  5. In the Main class, use Scanner to read the name, create instances of both Animal and Cat, and call speak() on each

Output Format

Animal speaks
Meow! I'm [name]

Sample I/O

Input:

Tom

Output:

Animal speaks
Meow! I'm Tom

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