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

Lab 8: Abstract Classes and Interfaces Overview: In this lab you will practice e

ID: 3730014 • Letter: L

Question

Lab 8: Abstract Classes and Interfaces Overview: In this lab you will practice extending abstract classes and implementing interfaces Both abstract classes and interfaces are ways of defining types within a type hierarchy, and neither can be directly instantiated. A. Provided Code You are provided with a zip file containing an abstract class Vehicle and an interface Reversible Thing. Vehicle is an abstract class that gives a partial specification of how vehicles behave. It contains two methods, one of which is abstract. The abstract method is refuel(). This is a way of saying that all vehicles need to be refueled, but that the refueling methods may vary among the subclasses of Vehicle (e.g. gas, diesel, electric, solar, vegetable oi...). So the abstract superclass gives no implementation. The implemented method is moveForward), which just contains a generic print statement about moving forward ReversibleThing is an interface containing one abstract method, reverse). Anything that implements ReversibleThing has to implement reverse. B. New Classes You need to create 3 new classes based on the following UML diagram. Vehicle

Explanation / Answer

Question1:

Vehicle newcar=new Car();//compilation error

newCar.reFuel();

It will result in error since Vehicle is abstract class we cannot directly create object to that class and also we cannot assign child class object to the parent

if parent is abstract.

Question 2:

newCar.moveForward();

As per above explanation we cannot create newCar() so we cannot call this function

Question 3:

Vehicle horse= new Horse();//compilation error

horse.refuel();

same explanation as question1

Question 4:

horse.moveForward();

Same explanation as question 2

Question 5:

horse.neigh()

as instance creation results in compilation error we cannot call neigh()

Question 6:

Horse horse2=new Horse();

horse2.neigh();

It will print Neigh by calling the neigh function in Horse class

Question 7:

ReversibleThing car2=new Car();

car.reverse();

will print putting car in reverse

Eventhough car2 is of type ReversibleThing it is holding the instance of type class Car so it will call reverse method in Car class

Question 8:

Car car3=new Car();

car3.moveForward();

it will print Pedal to the metal! by calling moveFOrward() in Car class

Question 9:

ReversibleThing drill = new CordlessDrill();

drill.reverse();

it will print Unscrewing Something

same explanation as question 7.

Question 10.

drill.moveForward();//will result in compilation error since CordlessDrill does not have that method

Question11:

ReversibleThing reverses[]={car2,drill};

for(ReversibleThing rt:reverses)

rt.reverse();

will print

putting car in reverse

Unscrewing Something

by calling their respective methods in their classes.

Question 12:

ReversibleThing reverses[]={newCar,drill};

for(ReversibleThing rt:reverses)

rt.reverse();

Will result in compilation error since we didnt have any variable newCar.