015-005-008

instanceof with Parent Type Variables

Medium

Problem Description

instanceof with Parent Type Variables

In this problem, you will create a program that defines a Vehicle class and its subclasses (Car, Motorcycle), assigns child class instances to parent type variables, checks actual types with instanceof, and displays the result to standard output.

Learning Objective: Understand instanceof checking when parent type variables hold child class instances

Overview

Create a program that assigns child class instances to parent type variables and checks actual types with instanceof.

Specifications

  • Define Vehicle class and its subclasses (Car, Motorcycle)
  • Assign each subclass instance to Vehicle type variable
  • Check actual type with instanceof and output appropriate message

Output Format

vehicle1 is a Car
vehicle2 is a Motorcycle

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