🎯 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:
Base Class - Vehicle: Create a base class named
Vehicle
with the following features:Private instance variables for
make
(String) andspeed
(int).A constructor that initializes
make
andspeed
.A method
showInfo()
that prints the make and speed of the vehicle.A method
accelerate()
that increases the speed by 5 units.
Subclass - Car: Create a subclass named
Car
that inherits fromVehicle
:Add an additional private instance variable for
fuelType
(String).A constructor that initializes
make
,speed
, andfuelType
.Override the
showInfo()
method to also display the fuel type.
Subclass - Bicycle: Create another subclass named
Bicycle
that also inherits fromVehicle
:Add an additional private instance variable for
hasBell
(boolean).A constructor that initializes
make
,speed
, andhasBell
.Override the
showInfo()
method to also display whether the bicycle has a bell.
Main Method: In your
main
method, perform the following tasks:Create an instance of
Car
and call itsshowInfo()
andaccelerate()
methods.Create an instance of
Bicycle
and call itsshowInfo()
andaccelerate()
methods.Create an array of
Vehicle
and populate it with variousCar
andBicycle
objects. Loop through the array and call theshowInfo()
method for each vehicle. Observe how the correctshowInfo()
method is called for each object, demonstrating polymorphism.
!🚚 Deliverables
Upload the following files in a zip folder that you have created:
Java class files
Screenshot of the Console with the code executing