Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application that demonstrates polymorphic features. The application mus

ID: 3853674 • Letter: W

Question

Write an application that demonstrates polymorphic features. The application must contain 4 classes ("Vehicle", "Car", "Truck", "PolymorphicApp"). "Car" and "Truck" inherit from "Vehicle" The "Vehicle" class must contain a method called "drive( )" The derived classes must override the "drive( )" method The "Vehicle" class must contain a constructor and a default implementation or the "drive( )" method. The subclasses must contain contructors and an implementation for the "drive( )" methods that override the "Vehicle" class version.

The application class, "PolymorphicApp" must perform the following.

1. Declare an array of three "Vehicle" references

2. Create an instance of each subclass (i.e., object of "Car" and "Truck"). There will be a total of 2 objects

3. Assign each array reference element to one of the objects

4. Create a loop that iterates through the array and invokes the "drive()" method

Note: The idea is that each call to "drive( )" will display a different message based on the type of object that is referenced.

Explanation / Answer


Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.

__________________

Vehicle.java

public class Vehicle {

// Zero argumented constructor

public Vehicle() {

}

// creating a method

public void drive() {

System.out.println("** Driving a Vehicle **");

}

}

____________________

Car.java

public class Car extends Vehicle {

// Zero argumented constructor

public Car() {

}

// Here we are overriding the super class drive() method

@Override

public void drive() {

System.out.println("** Driving a Car **");

}

}

_________________________

Truck.java

public class Truck extends Vehicle {

// Zero argumented constructor

public Truck() {

}

// Here we are overriding the super class drive() method

@Override

public void drive() {

System.out.println("** Driving a Truck **");

}

}

_______________________

PolymorphicApp.java

public class PolymorphicApp {

public static void main(String[] args) {

//Creating an Array of Vehicle class array of size 3

Vehicle vehicleArr[] = new Vehicle[3];

//Creating a Truck class instance

Truck truck1 = new Truck();

//Creating a Car class instance

Car car1 = new Car();

/* Assigning the references of subclasses of Vehicle class

* to the elements of vehicle class array

*/

vehicleArr[0] = truck1;

vehicleArr[1] = car1;

vehicleArr[2] = truck1;

//Using the for loop we are iterating over the array

for (int i = 0; i < vehicleArr.length; i++) {

vehicleArr[i].drive();

}

}

}

_______________________

Output:

** Driving a Truck **
** Driving a Car **
** Driving a Truck **


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote