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), reads vehicle types from standard input, creates instances accordingly, assigns them to parent type variables, checks actual types with instanceof, and displays the results.

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

Overview

Read two vehicle types (Car or Motorcycle) from standard input, create the corresponding instances, assign them to Vehicle type variables, and check actual types with instanceof.

Specifications

  • Define Vehicle class and its subclasses (Car, Motorcycle)
  • Read 2 lines from standard input and create the corresponding instance for each
  • Assign each instance to a Vehicle type variable
  • Check actual type with instanceof and output appropriate message

Input Format

type1 ("Car" or "Motorcycle")
type2 ("Car" or "Motorcycle")

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