Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

🎯 Objective

The purpose of this assignment is to deepen your understanding of inheritance in Object-Oriented Programming (OOP). You will create a class hierarchy for vehicles, demonstrating the use of inheritance.

Requirements:

  1. Base Class - Vehicle: Create a base class named Vehicle with the following features:

    • Private instance variables for make (String) and speed (int).

    • A constructor that initializes make and speed.

    • A method showInfo() that prints the make and speed of the vehicle.

    • A method accelerate() that increases the speed by 5 units.

  2. Subclass - Car: Create a subclass named Car that inherits from Vehicle:

    • Add an additional private instance variable for fuelType (String).

    • A constructor that initializes make, speed, and fuelType.

    • Override the showInfo() method to also display the fuel type.

  3. Subclass - Bicycle: Create another subclass named Bicycle that also inherits from Vehicle:

    • Add an additional private instance variable for hasBell (boolean).

    • A constructor that initializes make, speed, and hasBell.

    • Override the showInfo() method to also display whether the bicycle has a bell.

  4. Main Method: In your main method, perform the following tasks:

    • Create an instance of Car and call its showInfo() and accelerate() methods.

    • Create an instance of Bicycle and call its showInfo() and accelerate() methods.

    • Create an array of Vehicle and populate it with various Car and Bicycle objects. Loop through the array and call the showInfo() method for each vehicle. Observe how the correct showInfo() method is called for each object, demonstrating polymorphism.

!🚚  Deliverables

Upload the following files in a zip folder that you have created:

  1. Java class files

  2. Screenshot of the Console with the code executing

  • No labels