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.
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