015-002-003

Upcast: Assign to Parent Type

Medium

Problem Description

Upcast: Assign to Parent Type

In this problem, you will create a program that reads a cat's name from standard input, assigns a Cat object to an Animal type variable (upcasting), and displays the result of accessing the name and calling the sound method to standard output.

Learning Objective

Learn how to assign child class objects to parent class type variables.

Problem

Create an instance of the Cat class and assign it to an Animal type variable (upcasting). Then, use the Animal type variable to display the cat's name and call the sound method.

Input

Line 1: Cat name (string)

Output

Animal name: [name]
Animal makes sound.

Test Cases

※ Output examples follow programming industry standards

Normal case
Input:
Tama
Expected Output:
Animal name: Tama
Animal makes sound
Normal case
Input:
Mike
Expected Output:
Animal name: Mike
Animal makes sound

Your Solution

Current Mode: My Code
Animal.java🔒
Cat.java🔒
Main.java🔒
3/6 ファイル146B
⚠️警告
  • No main method found
import java.util.Scanner;

class Animal {
}
0 B / 5 MB

You have 10 free executions remaining